Submission #166592

# Submission time Handle Problem Language Result Execution time Memory
166592 2019-12-03T00:37:04 Z Lawliet Network (BOI15_net) C++17
100 / 100
879 ms 94788 KB
#include <bits/stdc++.h>

using namespace std;
typedef pair<int,int> pii;

const int MAXN = 500010;

int n;

int sub[MAXN];

vector< int > adj[MAXN];
vector< int > group[MAXN];

set< pii > s;

void DFS(int cur, int p, int t)
{
	if( adj[cur].size() == 1 )
	{
		sub[ cur ] = 1;

		group[ t ].push_back( cur );

		return;
	}

	for(int i = 0 ; i < adj[cur].size() ; i++)
	{
		int viz = adj[cur][i];

		if( viz == p ) continue;

		DFS( viz , cur , t );

		sub[ cur ] += sub[ viz ];
	}
}

int findPendantCentroid(int cur, int p, int s)
{
	for(int i = 0 ; i < adj[cur].size() ; i++)
	{
		int viz = adj[cur][i];

		if( viz == p ) continue;

		if( 2*sub[viz] > s ) return findPendantCentroid( viz , cur , s );
	}

	return cur;
}

void add(int i) { s.insert( { -group[i].size() , i } ); }

int main()
{
	scanf("%d",&n);

	for(int i = 1 ; i < n ; i++)
	{
		int U, V;
		scanf("%d %d",&U,&V);

		adj[ U ].push_back( V );
		adj[ V ].push_back( U );
	}

	int root = 0;

	for(int i = 1 ; i <= n ; i++)
		if( adj[i].size() > 1 ) root = i;

	DFS( root , root , 0 );

	int t = 0;
	int qtdLeafs = sub[root];
	int c = findPendantCentroid( root , root , sub[root] );

	for(int i = 0 ; i < adj[c].size() ; i++)
	{
		int viz = adj[c][i];

		DFS( viz , c , ++t );

		add( t );
	}

	int lastLeaf;

	printf("%d\n",( qtdLeafs + 1 )/2);

	while( qtdLeafs > 1 )
	{
		int curU = s.begin()->second; s.erase( s.begin() );
		int curV = s.begin()->second; s.erase( s.begin() );

		printf("%d %d\n",group[curU].back(),group[curV].back());

		lastLeaf = group[curU].back();

		qtdLeafs -= 2;

		group[ curU ].pop_back();
		group[ curV ].pop_back();

		if( !group[ curU ].empty() ) add( curU );
		if( !group[ curV ].empty() ) add( curV );
	}

	if( qtdLeafs == 1 )
	{
		int curU = s.begin()->second; s.erase( s.begin() );

		printf("%d %d\n",lastLeaf,group[curU].back());
	}
}

Compilation message

net.cpp: In function 'void DFS(int, int, int)':
net.cpp:28:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0 ; i < adj[cur].size() ; i++)
                  ~~^~~~~~~~~~~~~~~~~
net.cpp: In function 'int findPendantCentroid(int, int, int)':
net.cpp:42:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0 ; i < adj[cur].size() ; i++)
                  ~~^~~~~~~~~~~~~~~~~
net.cpp: In function 'int main()':
net.cpp:80:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0 ; i < adj[c].size() ; i++)
                  ~~^~~~~~~~~~~~~~~
net.cpp:58:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
  ~~~~~^~~~~~~~~
net.cpp:63:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&U,&V);
   ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 27 ms 23800 KB Output is correct
2 Correct 25 ms 23800 KB Output is correct
3 Correct 25 ms 23800 KB Output is correct
4 Correct 24 ms 23800 KB Output is correct
5 Correct 24 ms 23800 KB Output is correct
6 Correct 25 ms 23800 KB Output is correct
7 Correct 24 ms 23800 KB Output is correct
8 Correct 24 ms 23800 KB Output is correct
9 Correct 26 ms 23900 KB Output is correct
10 Correct 24 ms 23800 KB Output is correct
11 Correct 25 ms 23800 KB Output is correct
12 Correct 25 ms 23800 KB Output is correct
13 Correct 25 ms 23800 KB Output is correct
14 Correct 25 ms 23800 KB Output is correct
15 Correct 25 ms 23800 KB Output is correct
16 Correct 25 ms 23800 KB Output is correct
17 Correct 25 ms 23800 KB Output is correct
18 Correct 24 ms 23800 KB Output is correct
19 Correct 28 ms 23800 KB Output is correct
20 Correct 24 ms 23800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 23800 KB Output is correct
2 Correct 25 ms 23800 KB Output is correct
3 Correct 25 ms 23800 KB Output is correct
4 Correct 24 ms 23800 KB Output is correct
5 Correct 24 ms 23800 KB Output is correct
6 Correct 25 ms 23800 KB Output is correct
7 Correct 24 ms 23800 KB Output is correct
8 Correct 24 ms 23800 KB Output is correct
9 Correct 26 ms 23900 KB Output is correct
10 Correct 24 ms 23800 KB Output is correct
11 Correct 25 ms 23800 KB Output is correct
12 Correct 25 ms 23800 KB Output is correct
13 Correct 25 ms 23800 KB Output is correct
14 Correct 25 ms 23800 KB Output is correct
15 Correct 25 ms 23800 KB Output is correct
16 Correct 25 ms 23800 KB Output is correct
17 Correct 25 ms 23800 KB Output is correct
18 Correct 24 ms 23800 KB Output is correct
19 Correct 28 ms 23800 KB Output is correct
20 Correct 24 ms 23800 KB Output is correct
21 Correct 24 ms 23800 KB Output is correct
22 Correct 27 ms 24056 KB Output is correct
23 Correct 39 ms 24056 KB Output is correct
24 Correct 26 ms 24056 KB Output is correct
25 Correct 26 ms 24056 KB Output is correct
26 Correct 26 ms 23960 KB Output is correct
27 Correct 25 ms 23928 KB Output is correct
28 Correct 26 ms 23928 KB Output is correct
29 Correct 27 ms 23928 KB Output is correct
30 Correct 26 ms 23800 KB Output is correct
31 Correct 26 ms 23928 KB Output is correct
32 Correct 28 ms 23800 KB Output is correct
33 Correct 24 ms 23800 KB Output is correct
34 Correct 24 ms 23800 KB Output is correct
35 Correct 24 ms 23800 KB Output is correct
36 Correct 25 ms 23800 KB Output is correct
37 Correct 25 ms 23800 KB Output is correct
38 Correct 25 ms 23900 KB Output is correct
39 Correct 25 ms 23800 KB Output is correct
40 Correct 24 ms 23800 KB Output is correct
41 Correct 25 ms 23800 KB Output is correct
42 Correct 25 ms 23928 KB Output is correct
43 Correct 25 ms 23928 KB Output is correct
44 Correct 50 ms 23748 KB Output is correct
45 Correct 25 ms 23800 KB Output is correct
46 Correct 25 ms 23928 KB Output is correct
47 Correct 25 ms 23800 KB Output is correct
48 Correct 25 ms 23800 KB Output is correct
49 Correct 50 ms 23928 KB Output is correct
50 Correct 26 ms 23800 KB Output is correct
51 Correct 24 ms 23800 KB Output is correct
52 Correct 25 ms 23800 KB Output is correct
53 Correct 25 ms 23800 KB Output is correct
54 Correct 42 ms 23928 KB Output is correct
55 Correct 25 ms 23784 KB Output is correct
56 Correct 24 ms 23800 KB Output is correct
57 Correct 24 ms 23928 KB Output is correct
58 Correct 26 ms 23928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 23800 KB Output is correct
2 Correct 25 ms 23800 KB Output is correct
3 Correct 25 ms 23800 KB Output is correct
4 Correct 24 ms 23800 KB Output is correct
5 Correct 24 ms 23800 KB Output is correct
6 Correct 25 ms 23800 KB Output is correct
7 Correct 24 ms 23800 KB Output is correct
8 Correct 24 ms 23800 KB Output is correct
9 Correct 26 ms 23900 KB Output is correct
10 Correct 24 ms 23800 KB Output is correct
11 Correct 25 ms 23800 KB Output is correct
12 Correct 25 ms 23800 KB Output is correct
13 Correct 25 ms 23800 KB Output is correct
14 Correct 25 ms 23800 KB Output is correct
15 Correct 25 ms 23800 KB Output is correct
16 Correct 25 ms 23800 KB Output is correct
17 Correct 25 ms 23800 KB Output is correct
18 Correct 24 ms 23800 KB Output is correct
19 Correct 28 ms 23800 KB Output is correct
20 Correct 24 ms 23800 KB Output is correct
21 Correct 24 ms 23800 KB Output is correct
22 Correct 27 ms 24056 KB Output is correct
23 Correct 39 ms 24056 KB Output is correct
24 Correct 26 ms 24056 KB Output is correct
25 Correct 26 ms 24056 KB Output is correct
26 Correct 26 ms 23960 KB Output is correct
27 Correct 25 ms 23928 KB Output is correct
28 Correct 26 ms 23928 KB Output is correct
29 Correct 27 ms 23928 KB Output is correct
30 Correct 26 ms 23800 KB Output is correct
31 Correct 26 ms 23928 KB Output is correct
32 Correct 28 ms 23800 KB Output is correct
33 Correct 24 ms 23800 KB Output is correct
34 Correct 24 ms 23800 KB Output is correct
35 Correct 24 ms 23800 KB Output is correct
36 Correct 25 ms 23800 KB Output is correct
37 Correct 25 ms 23800 KB Output is correct
38 Correct 25 ms 23900 KB Output is correct
39 Correct 25 ms 23800 KB Output is correct
40 Correct 24 ms 23800 KB Output is correct
41 Correct 25 ms 23800 KB Output is correct
42 Correct 25 ms 23928 KB Output is correct
43 Correct 25 ms 23928 KB Output is correct
44 Correct 50 ms 23748 KB Output is correct
45 Correct 25 ms 23800 KB Output is correct
46 Correct 25 ms 23928 KB Output is correct
47 Correct 25 ms 23800 KB Output is correct
48 Correct 25 ms 23800 KB Output is correct
49 Correct 50 ms 23928 KB Output is correct
50 Correct 26 ms 23800 KB Output is correct
51 Correct 24 ms 23800 KB Output is correct
52 Correct 25 ms 23800 KB Output is correct
53 Correct 25 ms 23800 KB Output is correct
54 Correct 42 ms 23928 KB Output is correct
55 Correct 25 ms 23784 KB Output is correct
56 Correct 24 ms 23800 KB Output is correct
57 Correct 24 ms 23928 KB Output is correct
58 Correct 26 ms 23928 KB Output is correct
59 Correct 687 ms 61304 KB Output is correct
60 Correct 692 ms 54016 KB Output is correct
61 Correct 24 ms 23932 KB Output is correct
62 Correct 24 ms 23800 KB Output is correct
63 Correct 597 ms 54640 KB Output is correct
64 Correct 28 ms 24312 KB Output is correct
65 Correct 41 ms 25852 KB Output is correct
66 Correct 263 ms 39416 KB Output is correct
67 Correct 866 ms 79360 KB Output is correct
68 Correct 879 ms 69828 KB Output is correct
69 Correct 39 ms 25848 KB Output is correct
70 Correct 201 ms 44568 KB Output is correct
71 Correct 745 ms 94788 KB Output is correct
72 Correct 690 ms 89344 KB Output is correct
73 Correct 165 ms 31728 KB Output is correct
74 Correct 684 ms 53028 KB Output is correct
75 Correct 67 ms 29304 KB Output is correct
76 Correct 585 ms 74960 KB Output is correct
77 Correct 603 ms 75980 KB Output is correct
78 Correct 67 ms 27768 KB Output is correct
79 Correct 835 ms 62396 KB Output is correct
80 Correct 29 ms 24184 KB Output is correct
81 Correct 214 ms 40380 KB Output is correct
82 Correct 726 ms 73352 KB Output is correct
83 Correct 735 ms 52840 KB Output is correct
84 Correct 729 ms 53032 KB Output is correct
85 Correct 681 ms 52972 KB Output is correct
86 Correct 669 ms 54564 KB Output is correct
87 Correct 668 ms 54780 KB Output is correct