News Distribution题解 (并查集)
题目链接:https://codeforces.com/group/5yyKg9gx7m/contest/270506/problem/F 思路 很明显是个并查集,不过在并查集的基础上要统计每个节点的所有子结点的数量,最后遍历每个结点,输出其根节点的所有子节点数就ok。 # include <iostream> # include <cstdio> # include <cmath> # include <algorithm> # include <string> # include <map> # include <cstring> # include <climits> # include <vector> # include <queue> # include <stack> # define ll long long using namespace std ; int N , M ; int fa [ 500001 ] ; //记录每个节点的当前根节点 int co [ 500001 ] ; //记录每个节点的子节点数+1(因为要包括自己) int find ( int x ) //寻找x的根节点 { if ( x == fa [ x ] ) return x ; else return fa [ x ] = find ( fa [ x ] ) ; } int main (