Submission #57131925


Source Code Expand

#include<bits/stdc++.h>
#ifdef xay5421
#define D(...) fprintf(stderr,__VA_ARGS__)
#define DD(...) D(#__VA_ARGS__ "="),debug_helper::debug(__VA_ARGS__),D("\n")
#include"/home/xay5421/debug.hpp"
#else
#define D(...) ((void)0)
#define DD(...) ((void)0)
#endif
#define pb push_back
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define each(x,v) for(auto&x:v)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
template<class T>void rd(T&x){int f=0,c;while(!isdigit(c=getchar()))f^=!(c^45);x=(c&15);while(isdigit(c=getchar()))x=x*10+(c&15);if(f)x=-x;}
template<class T>void pt(T x,int c=-1){if(x<0)putchar('-'),x=-x;if(x>9)pt(x/10);putchar(x%10+48);if(c!=-1)putchar(c);}
using namespace std;
using LL=long long;
using ULL=unsigned long long;
const int N=250005;
int n;
vector<int>e[N],VV[N];
int sz[N],rt,f[N];
void dfs1(int k1,int k2){
	sz[k1]=1;
	for(auto&x:e[k1])if(x!=k2){
		dfs1(x,k1);
		sz[k1]+=sz[x];
		f[k1]=max(f[k1],sz[x]);
	}
	f[k1]=max(f[k1],n-sz[k1]);
	if(f[k1]<f[rt])rt=k1;
}
vector<int>V;
void dfs2(int k1,int k2){
	int v=0;
	for(auto&x:e[k1])if(x!=k2&&(sz[x]&1)){
		assert(!v);
		v=x;
	}
	if(v)dfs2(v,k1);
	for(auto&x:e[k1])if(x!=v&&x!=k2){
		dfs2(x,k1);
	}
	V.pb(k1);
}
int main(){
#ifdef xay5421
	freopen("a.in","r",stdin);
#endif
	rd(n);
	rep(i,2,n){
		int u,v;
		rd(u),rd(v);
		e[u].pb(v),e[v].pb(u);
	}
	f[0]=1e9;
	dfs1(1,0);
	dfs1(rt,0);
	priority_queue<pair<int,int> >pq[2];
	for(auto&x:e[rt]){
		V.clear();
		dfs2(x,rt);
		reverse(V.begin(),V.end());
		VV[x]=V;
		pq[sz[x]&1].emplace(sz[x],x);
	}
	rep(_,1,n/2-1){
		assert(!pq[1].empty());
		auto u=pq[1].top();
		pq[1].pop();
		assert(!pq[0].empty());
		auto v=pq[0].top();
		pq[0].pop();
		printf("%d %d\n",VV[u.second].back(),VV[v.second].back());
		VV[u.second].pop_back();
		VV[v.second].pop_back();
		if(--u.first){
			pq[u.first&1].push(u);
		}
		if(--v.first){
			pq[v.first&1].push(v);
		}
	}
	assert(!pq[1].empty());
	printf("%d %d\n",pq[1].top().second,rt);
	return 0;
}

Submission Info

Submission Time
Task D - Keep Perfectly Matched
User xay5421
Language C++ 20 (gcc 12.2)
Score 800
Code Size 2088 Byte
Status AC
Exec Time 173 ms
Memory 39788 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 800 / 800
Status
AC × 4
AC × 40
Set Name Test Cases
Sample 00-sample-001.txt, 00-sample-002.txt, 00-sample-003.txt, 00-sample-004.txt
All 00-sample-001.txt, 00-sample-002.txt, 00-sample-003.txt, 00-sample-004.txt, 01-001.txt, 01-002.txt, 01-003.txt, 01-004.txt, 01-005.txt, 01-006.txt, 01-007.txt, 01-008.txt, 01-009.txt, 01-010.txt, 01-011.txt, 01-012.txt, 01-013.txt, 01-014.txt, 01-015.txt, 01-016.txt, 01-017.txt, 01-018.txt, 01-019.txt, 01-020.txt, 01-021.txt, 01-022.txt, 01-023.txt, 01-024.txt, 01-025.txt, 01-026.txt, 01-027.txt, 01-028.txt, 01-029.txt, 01-030.txt, 01-031.txt, 01-032.txt, 01-033.txt, 01-034.txt, 01-035.txt, 01-036.txt
Case Name Status Exec Time Memory
00-sample-001.txt AC 3 ms 3644 KiB
00-sample-002.txt AC 3 ms 3636 KiB
00-sample-003.txt AC 3 ms 3764 KiB
00-sample-004.txt AC 3 ms 3760 KiB
01-001.txt AC 3 ms 3736 KiB
01-002.txt AC 3 ms 3696 KiB
01-003.txt AC 3 ms 3672 KiB
01-004.txt AC 3 ms 3796 KiB
01-005.txt AC 3 ms 3644 KiB
01-006.txt AC 154 ms 28024 KiB
01-007.txt AC 133 ms 28316 KiB
01-008.txt AC 173 ms 34736 KiB
01-009.txt AC 126 ms 20300 KiB
01-010.txt AC 140 ms 25980 KiB
01-011.txt AC 124 ms 20280 KiB
01-012.txt AC 148 ms 25532 KiB
01-013.txt AC 119 ms 27104 KiB
01-014.txt AC 117 ms 20420 KiB
01-015.txt AC 122 ms 20400 KiB
01-016.txt AC 136 ms 26464 KiB
01-017.txt AC 139 ms 27124 KiB
01-018.txt AC 115 ms 20328 KiB
01-019.txt AC 119 ms 20120 KiB
01-020.txt AC 133 ms 25400 KiB
01-021.txt AC 132 ms 27056 KiB
01-022.txt AC 127 ms 20288 KiB
01-023.txt AC 140 ms 27492 KiB
01-024.txt AC 117 ms 27308 KiB
01-025.txt AC 157 ms 30468 KiB
01-026.txt AC 145 ms 31032 KiB
01-027.txt AC 128 ms 23932 KiB
01-028.txt AC 116 ms 20528 KiB
01-029.txt AC 124 ms 23404 KiB
01-030.txt AC 97 ms 20448 KiB
01-031.txt AC 108 ms 20576 KiB
01-032.txt AC 107 ms 20620 KiB
01-033.txt AC 126 ms 24320 KiB
01-034.txt AC 108 ms 20556 KiB
01-035.txt AC 129 ms 20340 KiB
01-036.txt AC 65 ms 39788 KiB