国王的烦恼 Posted on 2019-03-20 | Edited on 2019-04-01 | In 蓝桥杯 | Views: 题意n 个点 m 条边,每条边有一个权值表示该边消失的时间,求有新的点不联通的时刻的数量。 题解以时间从大到小排序,用并查集反向建图,注意同时刻只能计一次。 代码12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273#include <bits/stdc++.h>#define forl(i, l, r) for (int i = l; i <= r; i++)#define forr(i, r, l) for (int i = r; i >= l; i--)#define for1(i, n) for (int i = 1; i <= n; i++)#define for0(i, n) for (int i = 0; i < n; i++)#define meminf(a) memset(a, inf, sizeof(a))#define mem_1(a) memset(a, -1, sizeof(a))#define mem0(a) memset(a, 0, sizeof(a))#define inlld(lld) scanf("%lld", &lld)#define inlf(f) scanf("%lf", &f)#define ind(d) scanf("%d", &d)#define ins(s) scanf("%s", s)#define mp make_pair#define pb push_back#define fi first#define se secondtypedef std::pair<long long, long long> pll;typedef std::vector<long long> vll;typedef std::pair<int, int> pii;typedef unsigned long long ull;typedef std::vector<int> vii;typedef long double db;typedef long long ll;const db pi = acos((db)-1);const ll inf =0x3f3f3f3f;const ll mod = 1e9+7;const int N = 1.1e4;const db eps = 1e-8;using namespace std;int sign(db a) { return a < -eps ? -1 : a > eps; }int db_cmp(db a, db b){ return sign(a-b); }int uni[N];int find_r(int x){ if(uni[x]==x)return x; else return uni[x]=find_r(uni[x]);}bool merge(int a,int b){ int fa=find_r(a),fb=find_r(b); if(fa==fb)return 0; uni[fa]=fb; return 1;}struct edg{ int u,v,w; bool operator < (const edg y)const{ return w>y.w; }}edge[10*N];int main() {#ifdef PerpEternal // freopen("/Users/perpeternal/Documents/Sketch/data/in.dat", "r", stdin); // freopen("/Users/perpeternal/Documents/Sketch/data/out.dat", "w", stdout);#endif //ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,m; scanf("%d%d",&n,&m); for1(i,n)uni[i]=i; for0(i,m)scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w); sort(edge,edge+m); int ans=0,time=0; for0(i,m){ // cout<<edge[i].u<<' '<<edge[i].v<<' '<<edge[i].w<<endl; if(merge(edge[i].u,edge[i].v)){ if(edge[i].w!=time){ time=edge[i].w; ans++; } } } printf("%d\n",ans); return 0;}