이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")/**/
#include<bits/stdc++.h>
/**/
//typedef int ll;
typedef long long ll;
typedef unsigned long long ull;
typedef std::string str;
//typedef vector<std::vector<ll>> vv;
/*typedef __int128 vll;
typedef unsigned __int128 uvll;*/
#define llll std::pair<ll , ll>
#define pb push_back
#define pf push_front
#define halo cout << "hello\n"
#define fi first
#define sec second
#define vv(x) std::vector<std::vector<x>>
#define all(a) a.begin() , a.end()
const ll limit = 1e9+7;
const ll ous = 5e5 + 7;
const ll dx[4] = {-1 , 0 , 1 , 0} , dy[4] = {0,1,0,-1};
template <typename T> std::string type_name();
ll n , m;std::vector<ll> ar;
std::vector<ll> adj[ous] , lc(ous) , v;
ll cnt = 0;
ll finlevcnt(ll crt , ll prv = 0 , ll crp = 0){
for(auto j:adj[crt]){
if(j != prv){
ll o = finlevcnt(j , crt , lc[crt] - lc[j] + crp);
if(o){
return o;
}
}
}
if(adj[crt].size() < 2){
return 0;
}
ll kd = (cnt + 1)/2 >= crp;
for(auto j:adj[crt]){
if(kd == 0){
break;
}
if(j != prv){
if(lc[j] > (cnt + 1)/2){
kd =0;
}
}
}
//std::cout << crt << " " << crp << " " << lc[3] << " " << kd << "\n";
return(kd ?crt :0);
}
ll dfs(ll crt , ll prv){
ll k = 1;
for(auto j:adj[crt]){
if(j != prv){
k = 0;
lc[crt] += dfs(j , crt);
}
}
if(k || adj[crt].size() < 2){
lc[crt] += 1;
cnt++;
}
return lc[crt];
}
void fndlv(ll crt , ll prv){
if(adj[crt].size() < 2){
v.pb(crt);
return;
}
for(auto j:adj[crt]){
if(j != prv){
fndlv(j , crt);
}
}
}
void solve(){
ll n;std::cin >> n;
for(ll i = 0;n-1>i;i++){
ll x , y;std::cin >> x >> y;
adj[x].pb(y);
adj[y].pb(x);
}
dfs(1 , 0);
ll o = finlevcnt(1);
for(auto j:adj[o]){
fndlv(j , o);
}
ll tl = 0;
//std::cout << o << "\n";
ll ps = ((cnt + 1) / 2) * 2;
ll ans [ps];
for(ll i = 0;ps>i;i++){
ans[tl] = v[i % v.size()];
tl+=2;
if(tl == ps){
tl = 1;
}
}
std::cout << ps / 2 << "\n";
for(ll i = 0;ps>i;i+=2){
std::cout << ans[i] << " " << ans[i+1] << "\n";
}
return;/**/
}
signed main(){
std::ios_base::sync_with_stdio(false);std::cin.tie(NULL);
ll t=1;
//std::cin >> t;
ll o = 1;
while(t--){
//cout << "Case " << o++ << ":\n";
solve();
}
return 0;
}/**/
컴파일 시 표준 에러 (stderr) 메시지
net.cpp:5:78: warning: "/*" within comment [-Wcomment]
5 | #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")/**/
|
net.cpp: In function 'int main()':
net.cpp:114:8: warning: unused variable 'o' [-Wunused-variable]
114 | ll o = 1;
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |