#include<bits/stdc++.h> usingnamespace std; constint maxn = 1e5 + 5; structnode{ int to, id; }; vector<node> e[maxn]; int x[maxn], y[maxn]; int vis[maxn]; int ans[maxn]; int l, r; voiddfs(int u,int fa){ int sz = e[u].size(); for (int i = 0; i < sz;i++) { int v = e[u][i].to; int id = e[u][i].id; if(v==fa||vis[id]) continue; ans[id] = l++; } for (int i = 0;i<sz;i++) { int v = e[u][i].to; int id = e[u][i].id; if(v==fa||vis[id]) continue; vis[id]++; dfs(v, u); } } intmain(){ int n; scanf("%d", &n); for (int i = 0; i < n - 1;i++) { scanf("%d %d", &x[i], &y[i]); e[x[i]].push_back((node){y[i], i}); e[y[i]].push_back((node){x[i], i}); } l = 0, r = n - 2; int d = 0; int m = 0; for (int i = 1; i <= n;i++) { int sz = e[i].size(); if(sz>d) { d = sz; m = i; } } dfs(m, -1); for (int i = 0; i < n-1;i++) { printf("%d\n", ans[i]); } return0; }
#include<bits/stdc++.h> usingnamespace std; constint maxn = 2e5 + 100; set<pair<int,int> > s; vector<int> v[maxn]; bool del[maxn]; int deg[maxn],occ[maxn]; voidremove(int x) { if (del[x]) return; s.erase({deg[x],x}); del[x]=1; for (int u:v[x]) { if (!del[u]) { s.erase({deg[u],u}); deg[u]--; s.insert({deg[u],u}); } } }
intmain() { int n, m,sq; scanf("%d%d",&n,&m); sq = ceil(sqrt(n)); while (m--) { int a,b; scanf("%d%d",&a,&b); v[a].push_back(b); v[b].push_back(a); deg[a]++; deg[b]++; }
for (int i=1;i<=n;i++) s.insert({deg[i],i}); vector<int> ans; while (!s.empty()) { auto p=*s.begin(); s.erase(s.begin()); if (p.first+1>=sq)//度数大于等于sq-1 { printf("2\n"); vector<int> d({p.second}); occ[p.second]=1; while (1) { pair<int,int> nex(1e9,0); for (int u:v[d.back()]) { if (!del[u]) nex=min(nex,make_pair(occ[u],u)); } if (nex.first) { printf("%d\n",(int)d.size()-nex.first+1); for (int i=nex.first-1;i<d.size();i++) printf("%d ",d[i]); return0; } d.push_back(nex.second); occ[nex.second]=d.size(); } } //度数小于sq-1 ans.push_back(p.second); remove(p.second);//删除所有邻边 for (int u:v[p.second])//双向删除 remove(u); } printf("1\n"); for (int i=0;i<sq;i++) printf("%d ",ans[i]); }
#include<bits/stdc++.h> usingnamespace std; constint maxn = 2e5 + 100; vector<int> e[maxn]; int vis[maxn]; constlonglong mod = 1e9 + 7; #define add(x, y) (x + y) % mod; intdfs(int u,int fa){ int sz = e[u].size(); longlong ret = 1; vis[u]++; for (int i = 0; i < sz;i++) { int v = e[u][i]; if(v==fa||vis[v]){ continue; } ret += dfs(v, u); } return ret; } longlongqpow(longlong x,int k){ longlong ret = 1; while(k) { if(k&1) ret = ret * x % mod; x = x * x % mod; k >>= 1; } return ret; } intmain(){ int n, k; scanf("%d%d", &n, &k); for (int i = 1; i <= n - 1;i++) { int x, y, w; scanf("%d%d%d", &x, &y, &w); if(!w) { e[x].push_back(y); e[y].push_back(x); } } longlong sum = 0; for (int i = 1; i <= n; i++) { if (vis[i]) continue; longlong cnt = dfs(i, -1); longlong x = qpow(cnt, k); sum = add(sum, x); } longlong ans = qpow(n, k); ans = (ans - sum + mod) % mod; cout << ans << endl; }