nyoj 925

来源:互联网 发布:清远招聘网络总监 编辑:程序博客网 时间:2024/04/29 13:46

题目链接: 点这里
如果nyoj登不上的话也可以来这里提交 : 题目链接

题意: 略。
思路:反向运用并查集,按照天数从大到小排序,依次计算,用并查集维护。

AC代码:

#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <stack>#include <queue>#include <vector>#include <cmath>#include <map>#include <set>#define ll long long#define llu unsigned long longusing namespace std;const int maxn = 100010;const double eps = 1e-10;const double PI  = acos(-1.0);struct node {    int x,y;    int d;}p[maxn];int Scan()     //输入外挂{    int res=0,ch,flag=0;    if((ch=getchar())=='-')        flag=1;    else if(ch>='0'&&ch<='9')        res=ch-'0';    while((ch=getchar())>='0'&&ch<='9')        res=res*10+ch-'0';    return flag?-res:res;}void Out(int a)    //输出外挂{    if(a>9)        Out(a/10);    putchar(a%10+'0');}int n,m;int f[maxn];int Find(int x) {    int r = x;    while(r != f[r]) {        r = f[r];    }    int i,j=x;    while(f[j]!=r) {        i = f[j];        f[j] = r;        j = i;    }    return r;}bool cmp(node a,node b) {    return a.d > b.d;}int main(){    while(scanf("%d%d",&n,&m)!=EOF) {        for(int i=1;i<=n;i++) f[i] = i;        for(int i=1;i<=m;i++) {            p[i].x = Scan();            p[i].y = Scan();            p[i].d = Scan();//            scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].d);        }        sort(p+1,p+m+1,cmp);        int ans = 0;        int i = 1;        while(i<=m) {            int j = i+1;            int fx = Find(p[i].x),fy = Find(p[i].y);            int t = 0;            if(fx != fy) {                ans++;                t = 1;                if(fx < fy) {                    f[fy] = fx;                }else {                    f[fx] = fy;                }            }            int F = 0;            while(p[j].d == p[i].d) {                int fx = Find(p[j].x),fy = Find(p[j].y);                if(fx != fy) {                    F = 1;                    if(fx < fy) {                        f[fy] = fx;                    }else {                        f[fx] = fy;                    }                }                j++;            }            i=j;            if(!t && F) ans++;//            cout<<ans<<"*****"<<endl;        }        Out(ans);        printf("\n");//        cout<<ans<<endl;    }    return 0;}
0 0
原创粉丝点击