并查集
2013-08-23 08:40
176 查看
SZU Problem(A70):Locked Boxes
Case Time Limit: 10000MS
Time Limit: 10000MS
Judger: Number Only Judger
which is the number of test cases. T test cases follow.
Each test case contains a single integer
- this is the number of boxes owned by Poseidon. The boxes (as well as their corresponding keys) are numbered from 1 to N. Next, there are N lines: the i+1st line contains a single integer - the number of the box which the ith key has been placed in.
SZU Problem(B36):Reading books
Case Time Limit: 10000MS
Time Limit: 10000MS
Judger: Number Only Judger
To finish reading the i-th book, it takes LRJ time[i] minutes. But some books are similar in the content. If the i-th book and the j-th book are similar, then if LRJ has finished reading the i-th book, it will take him only
minutes to finish reading the j-th book. Of course if LRJ has finished reading the j-th book, it will take him only
minutes to finish reading the i-th book. Now you are asked to tell LRJ the minimal total time to finish reading all the N books.
and
. N is the total number of books. M is the number of pairs which are similar.
Then the following N lines describe
.
Next comes M lines, each contains two integer (i,j), indicating that the i-th book and the j-th book are similar.
Input is ended with EOF.
SZU Problem(B64):Network Connections
Case Time Limit: 10000MS
Time Limit: 10000MS
Judger: Normal
Write a program which based on information input from a text file counts the number of successful and the number of unsuccessful answers to the questions of the kind : is computer
interconnected with computer
?
The number of computers in the network
(
);
A list of pairs of the form:
computer
computer
, where computer
and computer
are integers from
to
. A pair of this form shows that computer
and computer
get interconnected.
computer
computer
, where computer
and computer
are integers from
to
. A pair of this form stands for the question: is computer
interconnected with computer
?
There's a blank line between datasets.
Each pair is on a separate line. Pairs can appear in any order, regardless of their type. The log is updated after each pair of type (a) and each pair of type (b) is processed according to the current network configuration.
successfully answered questions and
unsuccessfully answered questions. The program prints these two numbers to the standard output on the same line, in the order: successful answers, unsuccessful answers, as shown in the sample output. Print a blank line between datasets.
SZU Problem(J21):Are they the same?
Case Time Limit: 10000MS
Time Limit: 10000MS
Judger: Normal
The number of the test case M (1<=M<=10000) , and what they are. Please read sample for more.
For more details, please read the sample.
Judge Info
Memory Limit: 32768KBCase Time Limit: 10000MS
Time Limit: 10000MS
Judger: Number Only Judger
Description
Poseidon has N boxes with lock. What's inside the box? (hum..., think by yourself) Each box can either be opened with its corresponding key or smashed. Poseidon has put the keys in some of the boxes. He remembers which key has been placed in which box. Poseidon wants to access to all of the boxes. However, he wants to destroy as few of them as possible. Please write a Program to help Poseidon to determine how many boxes have to be smashed.Input
The input will contains multiple test cases. The first line of the input is a single integerwhich is the number of test cases. T test cases follow.
Each test case contains a single integer
- this is the number of boxes owned by Poseidon. The boxes (as well as their corresponding keys) are numbered from 1 to N. Next, there are N lines: the i+1st line contains a single integer - the number of the box which the ith key has been placed in.
Output
For each input test case, you are to output a single integer - the minimal number of boxes to be smashed in order to access to all of the boxes.Sample Input
2 4 2 1 2 4 4 2 1 2 4
Sample Output
2 2
#include <stdio.h> #define N 1000010 int array ; // 用来记录箱子的钥匙在哪里 int search( int ); int main() { int t, n, i, a; int num = 0; // 计算需要打烂多少个箱子 scanf( "%d", &t ); while( t-- ) { scanf( "%d", &n ); // 初始化 for( i=1; i<=n; i++ ) array[i] = i; num = 0; // 输入箱子的钥匙在哪里并判断它的根是谁。。 for( i=1; i<=n; i++ ) { scanf( "%d", &a ); array[i] = search(a); if( array[i] == i ) num++; } printf( "%d\n", num ); } return 0; } int search( int i ) { int key = i; // 找根 while( array[i] != i ) i = array[i]; // 路径压缩 int root = i; int t = key; int temp; while( array[t] != t ) { temp = array[t]; array[t] = root; t = temp; } return root; }
SZU Problem(B36):Reading books
Judge Info
Memory Limit: 32768KBCase Time Limit: 10000MS
Time Limit: 10000MS
Judger: Number Only Judger
Description
In the summer vacation, LRJ wants to improve himself in computer science. So he finds out N books of computer science in the school library. The books are numbered from 0 to N-1.To finish reading the i-th book, it takes LRJ time[i] minutes. But some books are similar in the content. If the i-th book and the j-th book are similar, then if LRJ has finished reading the i-th book, it will take him only
minutes to finish reading the j-th book. Of course if LRJ has finished reading the j-th book, it will take him only
minutes to finish reading the i-th book. Now you are asked to tell LRJ the minimal total time to finish reading all the N books.
Input
The first line contains two integersand
. N is the total number of books. M is the number of pairs which are similar.
Then the following N lines describe
.
Next comes M lines, each contains two integer (i,j), indicating that the i-th book and the j-th book are similar.
Input is ended with EOF.
Output
For each test case, just output the minimal total time on a single line.Sample Input
2 1 6 10 0 1 3 2 1 2 3 0 1 1 2 3 1 2 4 6 0 1
Sample Output
11 3 10
#include <stdio.h> #define N 110 #define M 5000 int time ; int array[M]; int sum ; // 按秩分配 int search( int ); int main() { int n, m; int i; int a, b; int total; while( scanf("%d%d", &n, &m ) != EOF ) { total = 0; for( i=0; i<n; i++ ) { scanf( "%d", &a ); time[i] = a; sum[i] = time[i]/2; } for( i=0; i<n; i++ ) array[i] = i; i = 0; while( i < m ) { scanf( "%d%d", &a, &b ); // 记得比较祖先~~~ a = search(a); b = search(b); i++; // 记得考虑两者拥有共同祖先的时候!!! if( a == b ) continue; if( time[a] <= time[b] ) { array[b] = a; sum[a] += sum[b]; } else { array[a] = b; sum[b] += sum[a]; } } for( i=0; i<n; i++ ) if( array[i] == i ) { sum[i] = sum[i] - time[i]/2 + time[i]; total += sum[i]; } printf( "%d\n", total ); } return 0; } int search( int i ) { int t = i; // 找根 while( array[i] != i ) i = array[i]; // 路径压缩 int root = i; int temp; while( array[t] != t ) { temp = array[t]; array[t] = root; t = temp; } return root; }
SZU Problem(B64):Network Connections
Judge Info
Memory Limit: 32768KBCase Time Limit: 10000MS
Time Limit: 10000MS
Judger: Normal
Description
Bob, who is a network administrator, supervises a network of computers. He is keeping a log connections between the computers in the network. Each connection is bi-directional. Two computers are interconnected if they are directly connected or if they are interconnected with the same computer. Occasionally, Bob has to decide, quickly, whether two given computers are connected, directly or indirectly, according to the log information.Write a program which based on information input from a text file counts the number of successful and the number of unsuccessful answers to the questions of the kind : is computer
interconnected with computer
?
Input
The first line of the input contains the number of dataset, and it's followed by a blank line. Each dataset is defined as follows:The number of computers in the network
(
);
A list of pairs of the form:
computer
computer
, where computer
and computer
are integers from
to
. A pair of this form shows that computer
and computer
get interconnected.
computer
computer
, where computer
and computer
are integers from
to
. A pair of this form stands for the question: is computer
interconnected with computer
?
There's a blank line between datasets.
Each pair is on a separate line. Pairs can appear in any order, regardless of their type. The log is updated after each pair of type (a) and each pair of type (b) is processed according to the current network configuration.
Output
For example, the input file illustrated in the sample below corresponds to a network of 10 computers and 7 pairs. There aresuccessfully answered questions and
unsuccessfully answered questions. The program prints these two numbers to the standard output on the same line, in the order: successful answers, unsuccessful answers, as shown in the sample output. Print a blank line between datasets.
Sample Input
1 10 c 1 5 c 2 7 q 7 1 c 3 9 q 9 6 c 2 5 q 7 5
Sample Output
1,2 血的教训,能用getchar(), 决不用scanf( "%c", &c );
#include <stdio.h> #define N 10005 int array ; int findroot( int ); int main() { int t, a, b, i, n, yes, no; char request; scanf( "%d", &t ); while( t-- ) { scanf( "%d\n", &n ); // 初始化啊,我是石头里蹦出来的,自己爸爸的是自己^^ for( i=1; i<=n; i++ ) array[i] = i; yes = no = 0; while( request = getchar() ) { // 手拉手我们是好朋友 if( request == 'c' ) { scanf( "%d%d", &a, &b ); if( findroot(a) != findroot(b) ) // 判断是否同祖先 array[findroot(a)] = findroot(b); } // 问呀问呀问问题 else if( request == 'q' ) { scanf( "%d%d", &a, &b ); if( a == b ) yes++; else { if( findroot(a) == findroot(b) ) yes++; else no++; } } else // 最后一个不判断 break; getchar(); } printf( "%d,%d\n", yes, no ); if( t ) printf( "\n" ); } return 0; }
SZU Problem(J21):Are they the same?
Judge Info
Memory Limit: 65536KBCase Time Limit: 10000MS
Time Limit: 10000MS
Judger: Normal
Description
You are given N relations about two words. Here relation means the two words can change from each other. Then I have m times queries about whether given two strings are the same. Every string has no more than 100 words. Every word has no more than 10 letters. To make two strings same, you can exchange the position of any two words in the string.Input
The number of the relations N (1<=N<=10000), and what they are.The number of the test case M (1<=M<=10000) , and what they are. Please read sample for more.
Output
For each case out put “yes” if the two string may be same, otherwise, output “no”. You should also output “Case + one space + No. + : + one space” before the answer.For more details, please read the sample.
Sample Input
3 i wo love ai you ni 5 ni wo i love you wo ai ni i love you ni ai wo ma i love you ni ai wo i love you ni wo
Sample Output
Case 1: no Case 2: yes Case 3: no Case 4: yes Case 5: no
#include <iostream> #include <map> #include <string> #include <stdio.h> #include <algorithm> using namespace std; const int N = 10005; int array ; int buffer ; int root ; int findroot( int ); int main() { int n, i, j, m, p, q, num = 0; string temp1, temp2; map<string, int> magic; while( scanf( "%d", &n ) != EOF ) { // 我的爸爸就是我^^ for( i=1; i<=n; ++i ) root[i] = i; for( i=1; i<=n; ++i ) { cin >> temp1; if( magic[temp1] == 0 ) magic[temp1] = i; cin >> temp2; if( magic[temp2] == 0 ) magic[temp2] = i; p = findroot(magic[temp1]); q = findroot(magic[temp2]); if( p != q ) root[p] = q; } scanf( "%d", &m ); for( i=1; i<=m; ++i ) { p = q = 0; while( cin >> temp1 ) { array[p++] = root[magic[temp1]]; if( getchar() == '\n' ) break; } while( cin >> temp1 ) { buffer[q++] = root[magic[temp1]]; if( getchar() == '\n' ) break; } if( p != q ) cout << "Case " << ++num << ": no" << endl; else { sort( array, array+p ); sort( buffer, buffer+q ); for( j=0; j<p; ++j ) if( array[j] != buffer[j] ) break; if( j == p ) cout << "Case " << ++num << ": yes" << endl; else cout << "Case " << ++num << ": no" << endl; } } } return 0; } int findroot( int i ) { int n = i; while( root != n ) n = root ; int t = i; int temp; while( root[t] != t ) { temp = root[t]; root[t] = n; t = temp; } return n; }
相关文章推荐
- 并查集简单应用
- poj 1988 多校联赛 带权并查集
- 【BZOJ1604】[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Treap+并查集
- HDU 2473 Junk-Mail Filter 并查集删除(FZU 2155盟国)
- 愚人并查集
- HDU 3038 How Many Answers Are Wrong (并查集)(需复习!)
- 一、并查集 (Union-Find Set)
- Codeforces Round 650E Clockwork Bomb(并查集)
- poj1182----带权并查集的应用
- poj 2492 A Bug's Life (并查集)
- POJ 2236 Wireless Network(并查集)
- hdu 1829(并查集)
- 并查集【转】
- 并查集
- LA4487 Exclusive-OR (加权并查集)
- Supermarket 并查集或贪心
- codeforce 437D 并查集+贪心
- [NOIp复习计划]:并查集
- nyist 751破坏城市(并查集)
- 【杭电oj】3635 - Dragon Balls(带权并查集,好题)