标签:
问题描述2
这一题去掉边后不连通反过来就是加上边后连同,所以按时间从大到小反着来。如果几条边时间相同的话,处理加上其中一条时其他当做还未出现。
AC代码:
# include <cstdio>
# include <cstring>
# include <algorithm>
using namespace std;
struct node{
int u;
int v;
int t;
};
node s[100010], temp_s[100010];
int temp_u[10010], temp_v[10010];
int visit[100010];
int father[10010];
int find_father(int x){
if(x==father[x]){
return x;
}
return father[x]=find_father(father[x]);
}
int compare(node a, node b){
return a.t>b.t;
}
int main(){
int m, n, i, j, k, ans, t, cur=0;
scanf("%d%d", &n, &m);
for(i=1; i<=m; i++){
scanf("%d%d%d", &s[i].u, &s[i].v, &s[i].t);
}
for(i=1; i<=n; i++){
father[i]=i;
}
sort(s+1, s+1+m, compare);
ans=0;
memset(visit, 0, sizeof(visit));
for(i=1; i<=m; i++){
t=s[i].t;
if(!visit[t]){
visit[t]=1;
cur=0;
for(j=i; j<=m; j++){
if(s[j].t==t){
temp_s[cur].u=s[j].u;
temp_s[cur].v=s[j].v;
temp_s[cur].t=s[j].t;
cur++;
}
else{
break;
}
}
int flage=0;
for(j=0; j<=cur-1; j++){
temp_u[j]=find_father(temp_s[j].u);
temp_v[j]=find_father(temp_s[j].v);
if(temp_u[j]!=temp_v[j]){
if(!flage){
ans++;
flage=1;
}
else{
break;
}
}
}
for(j=0; j<=cur-1; j++){
temp_u[j]=find_father(temp_s[j].u);
temp_v[j]=find_father(temp_s[j].v);
if(temp_u[j]!=temp_v[j]){
father[temp_u[j]]=temp_v[j];
}
}
}
}
printf("%d", ans);
return 0;
}标签:
原文地址:http://blog.csdn.net/shiwaigaoren12345/article/details/51335799