正文
增加 21 行删除 21 行
17 行未改动1819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211317 行未改动#define lint long long#define endl '\n'using namespace std;const int N = 2e5+5;struct edge{int u,v,w;bool operator<(edge x)const{return this->w > x.w;}}e[N<<1];struct edge1{int ne,to;}arr[N];int cnt,pre[N];int n,m;int f[N];int ans[N];bool vis[N];stack<int> s;inline int find(int x){if(f[x] == x) return x;return f[x] = find(f[x]);}inline void merge(int x,int y){int fx = find(x);int fy = find(y);if(fx != fy) f[fx] = fy;}void dfs(int x) {for(int i = pre[x]; i; i=arr[i].ne)if(!vis[arr[i].to]) dfs(arr[i].to);vis[x] = true;s.push(x);}signed main(){ios::sync_with_stdio(0);cin.tie(0);int T;cin >> T;while(T--){memset(pre,0,sizeof(pre));memset(vis,0,sizeof(vis));m = cnt = 0;cin >> n;int u,v,x,y;for(int i = 1; i < n; i++){cin >> u >> v >> x >> y;e[++m] = (edge){u,v,x};e[++m] = (edge){v,u,y};}sort(e+1,e+1+m);for(int i = 1; i <= n; i++)f[i] = i;int p = n;for(int i = 1; i <= m; i++){int fx = find(e[i].u),fy = find(e[i].v);if(fx != fy){arr[++cnt] = (edge1){pre[e[i].u],e[i].v};pre[e[i].u] = cnt;merge(e[i].u,e[i].v);}if(p == 0) break;}for(int i = 1; i <= n; i++)if(!vis[i]) dfs(i);for(int i = n; i >= 1; i--){ans[s.top()] = i;s.pop();}for(int i = 1; i <= n; i++)cout << ans[i] << " ";cout << endl;}return 0;}```17 行未改动1819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211317 行未改动#define lint long long#define endl '\n'using namespace std;const int N = 2e5+5;struct edge{int u,v,w;bool operator<(edge x)const{return this->w > x.w;}}e[N<<1];struct edge1{int ne,to;}arr[N];int cnt,pre[N];int n,m;int f[N];int ans[N];bool vis[N];stack<int> s;inline int find(int x){if(f[x] == x) return x;return f[x] = find(f[x]);}inline void merge(int x,int y){int fx = find(x);int fy = find(y);if(fx != fy) f[fx] = fy;}void dfs(int x) {for(int i = pre[x]; i; i=arr[i].ne)if(!vis[arr[i].to]) dfs(arr[i].to);vis[x] = true;s.push(x);}signed main(){ios::sync_with_stdio(0);cin.tie(0);int T;cin >> T;while(T--){memset(pre,0,sizeof(pre));memset(vis,0,sizeof(vis));m = cnt = 0;cin >> n;int u,v,x,y;for(int i = 1; i < n; i++){cin >> u >> v >> x >> y;e[++m] = (edge){u,v,x};e[++m] = (edge){v,u,y};}sort(e+1,e+1+m);for(int i = 1; i <= n; i++)f[i] = i;int p = n;for(int i = 1; i <= m; i++){int fx = find(e[i].u),fy = find(e[i].v);if(fx != fy){arr[++cnt] = (edge1){pre[e[i].u],e[i].v};pre[e[i].u] = cnt;merge(e[i].u,e[i].v);}if(p == 0) break;}for(int i = 1; i <= n; i++)if(!vis[i]) dfs(i);for(int i = n; i >= 1; i--){ans[s.top()] = i;s.pop();}for(int i = 1; i <= n; i++)cout << ans[i] << " ";cout << endl;}return 0;}```