File tree Expand file tree Collapse file tree 1 file changed +56
-0
lines changed Expand file tree Collapse file tree 1 file changed +56
-0
lines changed Original file line number Diff line number Diff line change 1+ #include < bits/stdc++.h>
2+ using namespace std ;
3+
4+ const int mxN=1e3 ;
5+ int n, s[mxN], c, m, s1;
6+ vector<int > adj[mxN];
7+
8+ void dfs1 (int u, int p=-1 ) {
9+ s[u]=1 ;
10+ for (int v : adj[u]) {
11+ if (v^p) {
12+ dfs1 (v, u);
13+ s[u]+=s[v];
14+ }
15+ }
16+ }
17+
18+ int dfs2 (int u, int p=-1 ) {
19+ for (int v : adj[u])
20+ if (v^p&&s[v]>n/2 )
21+ return dfs2 (v, u);
22+ return u;
23+ }
24+
25+ void dfs3 (int u, int w, int l, int r) {
26+ for (int i=0 , ss=0 ; i<adj[u].size (); ++i) {
27+ if (l<=i&&i<=r)
28+ continue ;
29+ int v=adj[u][i], k=find (adj[v].begin (), adj[v].end (), u)-adj[v].begin ();
30+ cout << u+1 << " " << v+1 << " " << w*(ss+1 ) << " \n " ;
31+ dfs3 (v, w, k, k);
32+ ss+=s[v];
33+ }
34+ }
35+
36+ int main () {
37+ ios::sync_with_stdio (0 );
38+ cin.tie (0 );
39+
40+ cin >> n;
41+ for (int i=1 , u, v; i<n; ++i) {
42+ cin >> u >> v, --u, --v;
43+ adj[u].push_back (v);
44+ adj[v].push_back (u);
45+ }
46+ dfs1 (0 );
47+ c=dfs2 (0 );
48+ dfs1 (c);
49+ sort (adj[c].begin (), adj[c].end (), [](const int &i, const int &j) {
50+ return s[i]>s[j];
51+ });
52+ while (s1<(n+1 )/3 )
53+ s1+=s[adj[c][m++]];
54+ dfs3 (c, 1 , m, adj[c].size ());
55+ dfs3 (c, s1+1 , 0 , m-1 );
56+ }
You can’t perform that action at this time.
0 commit comments