You are given a graph containing n vertices and m edges. Vertices are numbered from 0 to n-1. Initially, vertex i belongs to group i. We define a group A is connected to group B if and only if there exists at least an edge that connects the two vertices which belong to A and B respectively.
Now we will do q operations on this graph. The i-th operation is represented by an integer o_ioi.
In i-th operation, if there are no vertices belong to group o_ioi, nothing happens. Otherwise, for all vertices belong to a group which is connected to o_ioi, those vertices will belong to group o_ioi after this operation.
Now you are also given the q operations. Please answer each vertex belongs to which group after all operations.
The first line contains one integer t (1 \le t \le 1.6 \times 10^51≤t≤1.6×105) --- the number of test cases. The first line of each test contains two positive integers n and m (2 \le n \le 8 \times 10^52≤n≤8×105, 1 \le m \le 8 \times 10^51≤m≤8×105) --- the number of vertices and edges in the given graph. The following are m lines. Each of them contains two integers x and y, indicating there is an edge between vertex x and vertex y (0 \le x < y < n0≤x
The sum of m across the test cases doesn't exceed 8 \times 10^58×105.
And the sum of q across the test cases doesn't exceed 8 \times 10^58×105.
输出描述:
For each test, output one line contains n integers --- the i-th integer of them representing the group that the vertex i-1 belongs to.示例1
输入
复制
5 4 3 0 1 1 2 2 3 4 0 1 3 0 4 3 0 1 1 2 2 3 2 0 2 4 3 0 1 1 2 2 3 2 0 3 4 1 1 3 1 2 5 5 0 1 0 2 1 2 1 3 3 4 3 4 4 0输出
复制
0 0 0 0 2 2 2 2 0 0 3 3 0 1 2 3 0 0 0 0 0说明
Take the first test for example:
Initially, the four vertex is belong to 0, 1, 2, 3 respectively.
After the first operation, the four vertex is belong to 0, 0, 2, 3 respectively.
After the second operation, because there is no vertex belong to 1 now. The four vertex is still belong to 0, 0, 2, 3 respectively.
After the third operation, the four vertex is belong to 0, 0, 3, 3 respectively.
After the last operation, the four vertex is belong to 0, 0, 0, 0 respectively.
并查集 + list
听说list可以O(1)合并两个堆
代码:
#include
using namespace std; #define ll long long const int maxn = 8e5 + 5; using namespace std; vector G[maxn]; list lists[maxn]; int fa[maxn]; int n, m; void init(){ for(int i = 0; i < n; i++){ fa[i] = i; lists[i].clear(); lists[i].push_back(i); G[i].clear(); } } int find(int x){ if(x == fa[x]) return x; return fa[x] = find(fa[x]); } void dfs(int x){ if(x != find(x)){ return ; } int sz = lists[x].size(); while(sz--){ int u = lists[x].front(); lists[x].pop_front(); for(auto i : G[u]){ int uu = find(u); int ii = find(i); if(ii != uu){ fa[ii] = uu; lists[uu].splice(lists[uu].end(), lists[ii]); } } } } void solve(){ cin >> n >> m; init(); for(int i = 1; i <= m; i++){ int a, b; cin >> a >> b; G[a].push_back(b); G[b].push_back(a); } int k; cin >> k; for(int i = 1; i <= k; i++){ int b; cin >> b; dfs(b); } for(int i = 0; i < n; i++){ cout << find(i) << " "; } cout << endl; } int main(){ ios::sync_with_stdio(0); int t; cin >> t; while(t--){ solve(); } }