voidprocess(){ for (ll i = 1; i <= cnt; ++i) { ll a = e[i].a, b = e[i].b; a = find(a), b = find(b); if (a != b) par[a] = b; } for (ll i = 1; i <= n; ++i) { ll t = find(i); if (t != i)a[t]++; //如果父亲不是自己,那么和别人肯定有一条边联通,累加这个父节点连接的结点数 } for (ll i = 1; i <= n; ++i) { if (a[i] != 0)v.push_back(a[i] + 1); //如果该节点连了其他结点,那么就加上他自己压入vector里面 } ll res = 0; for(auto &t:v){//遍历所有的连通块 res += t*(t-1)/2; } ll all = n*(n-1)/2; cout<<all-res;//总的数量减去连通块的数量就是减少的村庄组合 }
intmain(){ cin.tie(nullptr), ios::sync_with_stdio(false); cin >> n >> m >> k; for (ll i = 1; i <= m; ++i) { ll ai, bi; cin >> ai >> bi; if (i <= k)continue; cnt++; e[cnt].a = ai; e[cnt].b = bi; } for (ll i = 1; i <= n; ++i)par[i] = i; process(); return0; }