Submission #220850

#TimeUsernameProblemLanguageResultExecution timeMemory
220850arnold518Network (BOI15_net)C++14
100 / 100
548 ms51956 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 5e5;
const int INF = 987654321;

int N;
vector<int> adj[MAXN+10], V;

void dfs(int now, int bef)
{
	int cnt=0;
	for(int nxt : adj[now])
	{
		if(nxt==bef) continue;
		dfs(nxt, now);
		cnt++;
	}
	if(cnt==0) V.push_back(now);
}

int main()
{
	int i, j;

	scanf("%d", &N);
	for(i=1; i<N; i++)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		adj[u].push_back(v);
		adj[v].push_back(u);
	}

	for(i=1; i<=N; i++) if(adj[i].size()>1) break;
	int root=i;
	dfs(root, root);
	
	printf("%d\n", (V.size()+1)/2);
	for(i=0; i<V.size()/2; i++) printf("%d %d\n", V[i], V[i+V.size()/2]);
	if(V.size()%2) printf("%d %d\n", V[0], V.back());
}

Compilation message (stderr)

net.cpp: In function 'int main()':
net.cpp:43:31: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::vector<int>::size_type {aka long unsigned int}' [-Wformat=]
  printf("%d\n", (V.size()+1)/2);
                 ~~~~~~~~~~~~~~^
net.cpp:44:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(i=0; i<V.size()/2; i++) printf("%d %d\n", V[i], V[i+V.size()/2]);
           ~^~~~~~~~~~~
net.cpp:28:9: warning: unused variable 'j' [-Wunused-variable]
  int i, j;
         ^
net.cpp:30:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
net.cpp:34: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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...