제출 #394630

#제출 시각아이디문제언어결과실행 시간메모리
394630saniyar_krmiNetwork (BOI15_net)C++14
0 / 100
14 ms24268 KiB
// Possible Overdose for this coder
#include <bits/stdc++.h>
#define put(x) cerr << #x << ": " << x << '\n'
#define line() cerr << "**************\n"

//#define F first
//#define S second
//#define mul(x, y) (((x) * (y)) %mod)
//#define bit(i, j) (((i)>>(j)) &1)
//#define left(id) ((id<<1) + 1)
//#define right(id) ((id<<1) + 2)
 
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;

const int maxn = 5e5 + 10;
int n;
vector <int> G[maxn];
bool mark[maxn];
int leaves[maxn], num;

void dfs(int v){
	mark[v] = 1;
	if(G[v].size() == 1)
		leaves[v] = 1;
	for(int u: G[v]){
		if(mark[u])
			continue;
		dfs(u);
		leaves[v] += leaves[u];
	}
}

vector <int> s[maxn];
void make(int v, int x){
	mark[v] = 1;
	if(G[v].size() == 1)
		s[x].push_back(v);

	for(int u: G[v]){
		if(mark[u])
			continue;
		make(u, x);
	}
}

int find(int v){
	mark[v] = 1;

	for(int u: G[v]){
		if(mark[u])
			continue;
		if(leaves[u] > num /2)
			return find(u);
	}

	return v;
}

int cnt = 0;
void solve(){
	dfs(1);
	num = leaves[1];

	fill(mark, mark+maxn, 0);
	int V = find(1);

	fill(mark, mark+maxn, 0);
	mark[V] = 1;
	for(int u: G[V])
		make(u, cnt++);
}

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

	cin >> n;
	if(n == 1)
		return cout << "0\n", 0;
	if(n == 2)
		return cout << "1\n1 2\n", 0;

	int u, v;
	for(int i=1; i<n; i++){
		cin >> u >> v;
		G[u].push_back(v), G[v].push_back(u);
	}
	solve();

	int res = (num+1) / 2;

	vector <int> ans(2 * res);
	int pt = 0;
	for(int i=0; i<cnt; i++)
		for(int vi: s[i]){
			ans[pt] = vi;
			pt += 2;
			if(pt == 2 * res)
				pt = 1;
		}
	
	if(ans.size() && ans.back() == 0)
		ans.back() = s[1][0];
	
	cout << res << '\n';
	for(int i=0; i<ans.size(); i+=2)
		cout << ans[i] << ' ' << ans[i+1] << '\n';
}

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

net.cpp: In function 'int main()':
net.cpp:110:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |  for(int i=0; i<ans.size(); i+=2)
      |               ~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...