zoukankan      html  css  js  c++  java
  • 体育场[带权并查集]

    Description
      观众席每一行构成一个圆形,每个圆形由300个座位组成,对300个座位按照顺时针编号1到300,且可以认为有无数多行。现在比赛的组织者希望观众进入场地的顺序可以更加的有趣,在门票上并没有规定每个人的座位,而是与这个圈中某个人的相对位置,可以坐在任意一行。
      门票上标示的形式如下:A B x 表示第B个人必须在A的顺时针方向x个位置(比如A坐在4号位子,x=2,则B必须坐在6号位子)。
      现在你就座位志愿者在入场口检票。如果拿到一张门票,与之前给定的矛盾,则被视为是假票,如果无矛盾,视为真票。现在给定该行入场观众的顺序,以及他们手中的门票,请问其中有多少假票?

    Input
      第一行两个数n(1<=n<=50,000)和m(1<=m<=100,000)。n表示人数。
      接下来m行,每行三个数A,B,x标示B必须坐在A的顺时针方向x个位置。(1<=A<=N), B(1<=B<=N), X(0<=X<300) (A!=B)
      以上字母含义如题所述。

    Output
      仅一个数,表示在m张票中有多少假票。

    Sample Input
    10 10
    1 2 150
    3 4 200
    1 5 270
    2 6 200
    6 5 80
    4 7 150
    8 9 100
    4 8 50
    1 7 100
    9 2 100

    Sample Output
    2

    Data Constraint

    Hint
    【样例解释】
      第5张和第10张是假票
    【数据范围】
      对于20%的数据:n<=100
      对于100%的数据:n<=50,000。
    .
    .
    .
    .
    .

    分析

    这里写图片描述
    .
    .
    .
    .

    程序:
    #include<iostream>
    using namespace std;
    int n,m,f[50001],d[50001],root1,root2,ans=0,a,b,x;
    int find(int w)
    {
        if (f[w]==w) return w;
        int t=f[w];
        f[w]=find(f[w]);
        d[w]=(d[w]+d[t])%300;
        return f[w];
    }
    
    int main()
    {
        cin>>n>>m;
        for (int i=1;i<=n;i++)
        {
            f[i]=i;
            d[i]=0;
        }
        for (int i=1;i<=m;i++)
        {
            cin>>a>>b>>x;
            root1=find(a);
            root2=find(b);
            if (root1!=root2)
            {
                f[root2]=root1;
                d[root2]=(-d[b]+x+d[a]+300)%300;
            } else
            if ((d[b]-d[a]+300)%300!=x%300) ans++;
        }
        cout<<ans;
        return 0;
    }
  • 相关阅读:
    List of the best open source software applications
    Owin对Asp.net Web的扩展
    NSwag给api加上说明
    'workspace' in VS Code
    unable to find valid certification path to requested target
    JMeter的下载以及安装使用
    exception disappear when forgot to await an async method
    Filter execute order in asp.net web api
    记录web api的request以及response(即写log)
    asp.net web api的源码
  • 原文地址:https://www.cnblogs.com/YYC-0304/p/9499935.html
Copyright © 2011-2022 走看看