Home Backend Development PHP Tutorial POJ 3107 - Godfather tree DP..vector should be used with caution..._PHP tutorial

POJ 3107 - Godfather tree DP..vector should be used with caution..._PHP tutorial

Jul 15, 2016 pm 01:21 PM
optimization good submit time out

The submission timed out... I really don't think there is much to optimize... The most I can do is change it back to the bottom-up BFS... but it's very troublesome and I have to remember a lot of things... I only found out after reading the discussion that it's mainly because of the vector... Change it to a handwritten linked list. .500MS passed,,,

Select any point as the root of the tree... Count the number of subtree elements at each point. For points that are not root... Subtract the number N of all points N from the number of elements of the current subtree num .as another child of the point...
Program:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<set>
#include<ctime>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#define oo 1000000007
#define ll long long
#define pi acos(-1.0)
#define MAXN 50005
using namespace std;  
struct node
{
      int x,y,next;
}line[MAXN*2];  
int n,AnsNum,AnsData,ans[MAXN],_next[MAXN];
bool used[MAXN];
void addline(int x,int y,int m)
{
      line[m].next=_next[x],_next[x]=m;
      line[m].x=x,line[m].y=y;
      return;
}
int dfs(int x)
{
      int MaxSub=0,num=0,t,k;
      k=_next[x];
      while (k) 
      {
           if (!used[line[k].y])
           {
                 used[line[k].y]=true;
                 t=dfs(line[k].y);
                 MaxSub=max(t,MaxSub);
                 num+=t;
                 used[line[k].y]=false;
           }
           k=line[k].next;
      }
      MaxSub=max(MaxSub,n-(num+1));
      if (MaxSub==AnsData) ans[++AnsNum]=x;
      else 
      if (MaxSub<AnsData)
      { 
             AnsData=MaxSub;
             AnsNum=0,ans[++AnsNum]=x;
      }
      return num+1;
}
int main()
{  
      int i,num; 
      while (~scanf("%d",&n))
      {
              memset(_next,0,sizeof(_next));
              for (i=1;i<n;i++)
              {
                     int x,y;
                     scanf("%d%d",&x,&y);
                     addline(x,y,i*2-1);
                     addline(y,x,i*2); 
              }
              memset(used,false,sizeof(used));
              AnsData=oo; used[1]=true;
              dfs(1);
              sort(ans+1,ans+1+AnsNum);
              printf("%d",ans[1]);
              for (i=2;i<=AnsNum;i++) printf(" %d",ans[i]);
              printf("\n");
      }
      return 0;
}
Copy after login


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477206.htmlTechArticleSubmission timeout... I really don’t think there is much to optimize... At most, change it back to bottom-up BFS... But it’s so troublesome to remember a lot of things. After reading the discussion, I found out that the main reason is vector.. Changed to handwritten linked list.. 500M...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
How to center the WPS Word table How to center the WPS Word table Mar 21, 2024 pm 02:21 PM

When using word in WPS, you often need to insert pictures, tables, etc., but if the inserted table is not centered, it will affect the beauty of the entire document. So how to set the centering of the WPS table? Today I will teach you how to make adjustments. The specific steps are as follows. Come and take a look! 1. The table in the picture is not in the middle of the page, which is not very beautiful. I want it to be centered. 2. First, right-click the mouse in the table (as shown in the picture). 3. Then click [Select All Tables] in the right-click menu (as shown by the red arrow in the figure). 4. After clicking, the table will be fully selected (as shown in the figure below). 5. At this time, click to open the [Start] tab of wps text (as shown by the red arrow in the figure). 6 o'clock

How does Meituan pay for overtime? Meituan's overtime compensation standards! How does Meituan pay for overtime? Meituan's overtime compensation standards! Mar 16, 2024 pm 07:55 PM

1. How will Meituan compensate for overtime? Meituan’s overtime compensation standards! Meituan’s overtime compensation rules are as follows: (1) Overtime when purchasing the Punctual Service: After selecting the Punctual Service, if the delivery rider fails to deliver on time, the system will automatically start the compensation process, and the amount of compensation will be determined based on the order details and the overtime duration. . (2) Ordinary timeout for non-purchased punctual products: 1. If the actual delivery time of the order is more than 10 minutes but less than 20 minutes later than the promised delivery time, 25% of the actual payment amount of the order will be compensated. 2. If the actual delivery time of the order is more than 20 minutes or less than 30 minutes later than the promised delivery time, 30% of the actual payment amount of the order will be compensated. 3. If the actual delivery time of the order is more than 30 minutes later than the promised delivery time, 50% of the actual payment amount of the order will be compensated. 4

C++ program optimization: time complexity reduction techniques C++ program optimization: time complexity reduction techniques Jun 01, 2024 am 11:19 AM

Time complexity measures the execution time of an algorithm relative to the size of the input. Tips for reducing the time complexity of C++ programs include: choosing appropriate containers (such as vector, list) to optimize data storage and management. Utilize efficient algorithms such as quick sort to reduce computation time. Eliminate multiple operations to reduce double counting. Use conditional branches to avoid unnecessary calculations. Optimize linear search by using faster algorithms such as binary search.

What to do if Meituan's errand delivery times out_How to deal with Meituan's errand delivery timeout What to do if Meituan's errand delivery times out_How to deal with Meituan's errand delivery timeout Mar 28, 2024 am 09:26 AM

1. First of all, when taking out food, you need to know whether the order is delivered by the merchant itself or by Meituan. Generally speaking, the order receiving efficiency of the merchant's self-delivery is low and timeouts often occur. However, since Meituan is not involved in the delivery, there is no timeout. Compensation principle. At this time, you can check to see if the order submitted contains a compensation clause for overtime delivery. If there is a relevant clause in the claim, there is no need to say more, the merchant will claim the claim. If there are no relevant rules, it is recommended that you leave a negative review or leave a message about the meal delivery service on the platform, or contact the merchant directly to complain about the delivery service so as to negotiate compensation. If you really can't negotiate, you can only admit that you are out of luck. Please pay more attention next time. 2. Overtime compensation model: The merchant promises a delivery time and a discount, and receives payment from the user

In-depth interpretation: Why is Laravel as slow as a snail? In-depth interpretation: Why is Laravel as slow as a snail? Mar 07, 2024 am 09:54 AM

Laravel is a popular PHP development framework, but it is sometimes criticized for being as slow as a snail. What exactly causes Laravel's unsatisfactory speed? This article will provide an in-depth explanation of the reasons why Laravel is as slow as a snail from multiple aspects, and combine it with specific code examples to help readers gain a deeper understanding of this problem. 1. ORM query performance issues In Laravel, ORM (Object Relational Mapping) is a very powerful feature that allows

MySQL transaction processing: the difference between automatic submission and manual submission MySQL transaction processing: the difference between automatic submission and manual submission Mar 16, 2024 am 11:33 AM

MySQL transaction processing: the difference between automatic submission and manual submission. In the MySQL database, a transaction is a set of SQL statements. Either all executions are successful or all executions fail, ensuring the consistency and integrity of the data. In MySQL, transactions can be divided into automatic submission and manual submission. The difference lies in the timing of transaction submission and the scope of control over the transaction. The following will introduce the difference between automatic submission and manual submission in detail, and give specific code examples to illustrate. 1. Automatically submit in MySQL, if it is not displayed

Laravel performance bottleneck revealed: optimization solution revealed! Laravel performance bottleneck revealed: optimization solution revealed! Mar 07, 2024 pm 01:30 PM

Laravel performance bottleneck revealed: optimization solution revealed! With the development of Internet technology, the performance optimization of websites and applications has become increasingly important. As a popular PHP framework, Laravel may face performance bottlenecks during the development process. This article will explore the performance problems that Laravel applications may encounter, and provide some optimization solutions and specific code examples so that developers can better solve these problems. 1. Database query optimization Database query is one of the common performance bottlenecks in Web applications. exist

Is there any compensation if Ele.me times out? Share Ele.me's overtime application compensation standards! Is there any compensation if Ele.me times out? Share Ele.me's overtime application compensation standards! Mar 15, 2024 pm 05:58 PM

1. Is there any compensation if Ele.me times out? Ele.me will provide compensation if it times out, but the prerequisite is that the user must purchase the Punctual Treasure or the merchant will give the Punctual Treasure as a gift when purchasing goods. Only with the Punctual Treasure can the compensation service be provided if the timeout expires. The latest version of Ele.me Category: Convenient Life Download Recommended Download: The latest version of Ele.me is a convenient takeout ordering platform. Through the Ele.me app, users can conveniently browse nearby restaurant menus, place orders and pay, and enjoy food delivery services. The latest version of Ele.me also adds a variety of special features, such as ordering, merchant discounts, etc., allowing users to enjoy delicious food more cheaply. The interface is simple and clear, the operation is simple and convenient, and it supports multiple payment methods, giving users a better experience. 2. Share Ele.me’s overtime application compensation standards! 1

See all articles