Submission #39846

#TimeUsernameProblemLanguageResultExecution timeMemory
39846MladenPNetwork (BOI15_net)C++14
100 / 100
826 ms69388 KiB
#include<bits/stdc++.h> #define STIZE(x) fprintf(stderr, "STIZE%d\n", x); #define PRINT(x) fprintf(stderr, "%s = %d\n", #x, x); #define NL(x) printf("%c", " \n"[(x)]); #define lld long long #define pii pair<int,int> #define pb push_back #define fi first #define se second #define all(a) begin(a),end(a) #define sz(a) int((a).size()) #define LINF LLONG_MAX #define INF INT_MAX #define EPS 1e-9 using namespace std; #define MAXN 500010 int n, root, centroid; int deg[MAXN]; int brlist[MAXN], cntLeaf; bool is_leaf[MAXN]; vector<int> adj[MAXN]; int dfs1(int cur, int pret) { if(brlist[cur] != 0) return brlist[cur]; if(is_leaf[cur]) return brlist[cur] = 1; for(auto x : adj[cur]) { if(x != pret) brlist[cur] += dfs1(x, cur); } return brlist[cur]; } int dfs2(int cur, int pret) { int maxi = 0; for(auto x : adj[cur]) { if(!is_leaf[x] && brlist[x] >= brlist[maxi] && x != pret) maxi = x; } if(brlist[maxi] > cntLeaf-cntLeaf/2) return dfs2(maxi, cur); else return cur; } int susedi; vector<int> lisce[MAXN]; void dfs3(int cur, int pret) { if(is_leaf[cur]) { lisce[susedi].pb(cur); return; } for(auto x : adj[cur]) { if(x != pret) dfs3(x, cur); if(cur == centroid) susedi++; } } priority_queue<pii> pq; int main() { scanf("%d", &n); if(n <= 2) return printf("1\n1 %d", n), 0; for(int i = 0; i < n-1; i++) { int x, y; scanf("%d%d", &x, &y); adj[x].pb(y); adj[y].pb(x); deg[x]++; deg[y]++; } for(int i = 1; i <= n; i++) { if(deg[i] == 1) is_leaf[i] = 1, cntLeaf++; else root = i; } dfs1(root, 0); centroid = dfs2(root, 0); dfs3(centroid, 0); for(int i = 0; i < susedi; i++) { pq.push({sz(lisce[i]), i}); } printf("%d\n", cntLeaf-cntLeaf/2); while(sz(pq) > 1) { pii vrh1 = pq.top(); pq.pop(); pii vrh2 = pq.top(); pq.pop(); int komp1 = vrh1.se, komp2 = vrh2.se; int rep1 = vrh1.fi, rep2 = vrh2.fi; printf("%d %d\n", lisce[komp1][--rep1], lisce[komp2][--rep2]); if(rep1) pq.push({rep1, komp1}); if(rep2) pq.push({rep2, komp2}); } if(pq.size() == 1) { int komp = pq.top().se; printf("%d %d\n", lisce[komp][0], centroid); } return 0; }

Compilation message (stderr)

net.cpp: In function 'int main()':
net.cpp:52:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
                    ^
net.cpp:55:40: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         int x, y; scanf("%d%d", &x, &y);
                                        ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...