Survey scores

(score.cpp 1s 256M)
small Hi school students a total of N, No. 1-N. School has just been a school of ancient poetry and prose proficiency tests. The school did not disclose test results, the
small Hi only get some gossip, such as X number of students score higher points score than the Y number of students S. Small Hi want to know the use of these messages, you can not be sentenced to
break out between the high and low scores of some two students?
Input
The first line contains three integers N, M and Q. N represents the total number of students, M represents the total number of messages in small Hi know, Q represents the number of small Hi want to ask.
Each of the three integers M row line, X, Y and S. X represents the number of students score higher than the score S Y number of students points.
The following two lines each integers Q, X and Y. Hi represents a small fraction wondering X number of students score higher than Y number of students somewhat.
To 100% of the data, 1 <= N, M, Q <= 100000 1 <= X, Y <= N -1000 <= S <= 1000
data to ensure that no contradiction.
Output
For each interrogation, if it is determined that X is not higher than the bit output Y-1. Otherwise the output X is higher than Y scores.
The Input the Sample
10. 5. 3 // number of students, number of messages, the number of query
1 10 2 1 // 2 higher than 10.
2. 3 10
. 4. 5 -10
. 5. 6 -10
2 10. 5
asks whether a 10 higher than 10 @ 1
. 5. 1
. 3. 5
the Sample the Output
-1 // X is not greater than y
Y20 points higher than 20 // x
0

sol: Weighted disjoint-set, first create disjoint-set to record the distance from each node to the father node.

Query, the query if the relationship between 3 and 5, 3 and 5 to see whether the same collection, if in use from -3 to 5 to the root of the root of the distance, is the answer, if not in, output -1 .

code show as below:

 1 #include<cstdio>
 2 using namespace std;
 3 int n,m,q,f[100001],v[100001];  
 4 int find(int x)
 5 {
 6     if(f[x]==x)return x;
 7     int now=find(f[x]);
 8     v[x]+=v[f[x]];f[x]=now;
 9     return now;
10 }
11 int main()
12 {
13     scanf("%d%d%d", & n-, & m, & Q);
 14      for ( int I = . 1 ; I <= n-; I ++) F [I] = I;
 15      for ( int I = . 1 , X, Y, Z; I <= m; I ++ )
 16      {
 . 17          Scanf ( " % D% D% D " , & X, & Y, & Z);
 18 is          int A = Find (X), B = Find (Y);
 . 19          IF (A == B) Continue ;
 20 is          F [B] = a; V [B] V = [x] + Z- V [y];
 21 is          // merge, b father to the right point to the weight value of x + x father is greater than y -y father's weight 
22 is      }
 23 is      for(int i=1,x,y;i<=q;i++)scanf("%d%d",&x,&y),printf("%d\n",find(x)==find(y)?v[y]-v[x]:-1);
24 } 

 

Guess you like

Origin www.cnblogs.com/cutepota/p/12593930.html