日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Zjnu Stadium HDU - 304 加权并查集

發布時間:2023/12/4 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Zjnu Stadium HDU - 304 加权并查集 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題意:

觀眾席圍成一圈。列的總數是300,編號為1–300,順時針計數,我們假設行的數量是無限的。將有N個人去那里。他對這些座位提出了要求:這意味著編號A的順時針X距離坐著編號B。例如:A在第4列,X是2,那么B必須在第6列(6=4+2)。現在你的任務是判斷請求是否正確。

題目:

In 12th Zhejiang College Students Games 2007, there was a new stadium built in Zhejiang Normal University. It was a modern stadium which could hold thousands of people. The audience Seats made a circle. The total number of columns were 300 numbered 1–300, counted clockwise, we assume the number of rows were infinite.
These days, Busoniya want to hold a large-scale theatrical performance in this stadium. There will be N people go there numbered 1–N. Busoniya has Reserved several seats. To make it funny, he makes M requests for these seats: A B X, which means people numbered B must seat clockwise X distance from people numbered A. For example: A is in column 4th and X is 2, then B must in column 6th (6=4+2).
Now your task is to judge weather the request is correct or not. The rule of your judgement is easy: when a new request has conflicts against the foregoing ones then we define it as incorrect, otherwise it is correct. Please find out all the incorrect requests and count them as R.

Input

There are many test cases:
For every case:
The first line has two integer N(1<=N<=50,000), M(0<=M<=100,000),separated by a space.
Then M lines follow, each line has 3 integer A(1<=A<=N), B(1<=B<=N), X(0<=X<300) (A!=B), separated by a space.

Output

For every case:
Output R, represents the number of incorrect request.

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

Hint

Hint:
(PS: the 5th and 10th requests are incorrect)

思路

1.由于題中明確給出,編號A的順時針X距離坐著編號B,故x<300,當我們用并查集找到一個祖宗節點時,我們可以將該點看作原點,將圈拉成直線看待。即可忽略成環。

2.b->a, a離”原點更近“

int f(int x) {if(x!=dp[x]){int t=dp[x];/*因為在遞歸找祖先的過程中,dp[x]的值會改變,而我們我們只需要找到距離x最近的點,即可得到dp[x]到祖宗節點的距離。所以要記錄t的值*/dp[x]=f(dp[x]);num[x]+=num[t];}return dp[x]; }
  • dp[b]->dp[a]
  • dp[v]=u;num[v]=num[a]+x-num[b];

    AC代碼

    #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int M=5e4+10; int dp[M],num[M]; int f(int x) {if(x!=dp[x]){int t=dp[x];dp[x]=f(dp[x]);/*因為在遞歸找祖先的過程中,dp[x]的值會改變,而我們我們只需要找到距離x最近的點,即可得到dp[x]到祖宗節點的距離。所以要記錄t的值*/num[x]+=num[t];}return dp[x]; } bool dfs(int a,int b,int x) {int u=f(a),v=f(b);/*a->u,b->v*/if(u==v){if(num[a]+x!=num[b])/*b->a,又起點為父節點*/return true;return false;}dp[v]=u;num[v]=num[a]+x-num[b];return false; } int main() {int m,n;while(~scanf("%d%d",&m,&n)){int ans=0;for(int i=0;i<m;i++){dp[i]=i;num[i]=0;}while(n--){int a,b,x;/*設起點為祖宗節點,則都指向起點b->a*/scanf("%d%d%d",&a,&b,&x);if(dfs(a,b,x))ans++;}printf("%d\n",ans);}return 0; }

    總結

    以上是生活随笔為你收集整理的Zjnu Stadium HDU - 304 加权并查集的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。