제출 #47124

#제출 시각아이디문제언어결과실행 시간메모리
47124MatheusLealVNetwork (BOI15_net)C++17
63 / 100
2052 ms112408 KiB
#include <bits/stdc++.h>
#define N 500050
#define f first
#define s second
using namespace std;
typedef pair<int, int> pii;

int n, qtd, tempo, ponte, cnt, dfsnum[N], dfslow[N], pai[N], ptr;

vector<int> grafo[N], save[N], leaf;

void dfs(int x)
{
	dfsnum[x] = dfslow[x] = ++cnt;
 
	for(int i = 0; i< grafo[x].size(); i++)
	{
		int v = grafo[x][i];
 
		if(!dfsnum[v])
		{
			pai[v] = x;
 
			dfs(v);
 
			if(dfslow[v] > dfsnum[x]) ponte ++;
 
			dfslow[x] = min(dfslow[v], dfslow[x]);
		}
 
		else if(v != pai[x]) dfslow[x] = min(dfsnum[v], dfslow[x]);
	}
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(0);

	cin>>n;

	for(int i = 1, a, b; i < n; i++)
	{
		cin>>a>>b;

		grafo[a].push_back(b);

		grafo[b].push_back(a);
	}

	for(int i = 1; i <= n; i++) save[i] = grafo[i];

	for(int i = 1; i <= n; i++) if(grafo[i].size() == 1) leaf.push_back(i);

	else ptr = i;

	if(leaf.size() % 2) leaf.push_back(ptr);

	for(int c = 0; c < 50; c++)
	{
		memset(dfsnum, 0, sizeof dfsnum);

		memset(dfslow, 0, sizeof dfslow);

		ponte = cnt = 0;

	  	random_shuffle(leaf.begin(), leaf.end());

	    for(int i = 0; i< leaf.size(); i += 2)
	    {
	      	int a = leaf[i], b = leaf[i + 1];

	     	grafo[a].push_back(b);

	      	grafo[b].push_back(a);
	    }	

	    dfs(1);

	    if(!ponte)
	    {
	    	cout<<(leaf.size() + 1)/2<<"\n";

		    for(int i = 0; i< leaf.size(); i += 2)
		    {
		      	int a = leaf[i], b = leaf[i + 1];

		     	cout<<a<<" "<<b<<"\n";
		    }	

		    break;
	  	}

	    for(int i = 0; i< leaf.size(); i += 2)
	    {
	     	int a = leaf[i], b = leaf[i + 1];

	       	grafo[a].pop_back();

	      	grafo[b].pop_back();
	    }    
	}
}

컴파일 시 표준 에러 (stderr) 메시지

net.cpp: In function 'void dfs(int)':
net.cpp:16:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i< grafo[x].size(); i++)
                 ~^~~~~~~~~~~~~~~~~
net.cpp: In function 'int main()':
net.cpp:68:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
      for(int i = 0; i< leaf.size(); i += 2)
                     ~^~~~~~~~~~~~~
net.cpp:83:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for(int i = 0; i< leaf.size(); i += 2)
                      ~^~~~~~~~~~~~~
net.cpp:93:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
      for(int i = 0; i< leaf.size(); i += 2)
                     ~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...