05 樹7:堆中的路徑 Description: 將一系列給定數字插入一個初始為空的小頂堆H[]。隨後對任意給定的下標i,列印從H[i]到根結點的路徑。 Input: 每組測試第1行包含2個正整數N和M(≤1000),分別是插入元素的個數、以及需要列印的路徑條數。下一行給出區間[ 10000, 10 ...
05-樹7:堆中的路徑
Description:
將一系列給定數字插入一個初始為空的小頂堆H[]。隨後對任意給定的下標i,列印從H[i]到根結點的路徑。
Input:
每組測試第1行包含2個正整數N和M(≤1000),分別是插入元素的個數、以及需要列印的路徑條數。下一行給出區間[-10000, 10000]內的N個要被插入一個初始為空的小頂堆的整數。最後一行給出M個下標。
Output:
對輸入中給出的每個下標i,在一行中輸出從H[i]到根結點的路徑上的數據。數字間以1個空格分隔,行末不得有多餘空格。
SampleInput:
5 3
46 23 26 24 10
5 4 3
SampleOutput:
24 23 10
46 23 10
26 10
Codes:
//#define LOCAL
#include <cstdio>
#define M 1010
#define S -10010
int i, cnt, A[M] = {S};
void insert(int a) {
for(i=++cnt; A[i/2]>a; i/=2) A[i] = A[i/2];
A[i] = a;
}
int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif
int a, n, m;
scanf("%d%d", &n, &m);
while(n--) { scanf("%d", &a); insert(a); }
while(m--) {
scanf("%d", &a);
printf("%d", A[a]); a /= 2;
while(a) { printf(" %d", A[a]); a /= 2; }
printf("\n");
}
return 0;
}
05-樹8:File Transfer.
Description:
We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer to another. Is it possible to send a file from any computer on the network to any other?
Input:
Each input file contains one test case. For each test case, the first line contains N(2, 10^4), the total number of computers in a network. Each computer in the network is then represented by a positive integer between 1 and N. Then in the following lines, the input is given in the format: I c1 c2 where I stands for inputting a connection between c1 and c2; or C c1 c2 where C stands for checking if it is possible to transfer files between c1 and c2; or S where S stands for stopping this case.
Output:
For each C case, print in one line the word "yes" or "no" if it is possible or impossible to transfer files between c1 and c2, respectively. At the end of each case, print in one line "The network is connected." if there is a path between any pair of computers; or "There are k components." where k is the number of connected components in this network.
SampleInput1:
5
C 3 2
I 3 2
C 1 5
I 4 5
I 2 4
C 3 5
S
SampleOutput1:
no
no
yes
There are 2 components.
SampleInput2:
5
C 3 2
I 3 2
C 1 5
I 4 5
I 2 4
C 3 5
I 1 3
C 1 5
S
SampleOutput2:
no
no
yes
yes
The network is connected.
Codes:
//#define LOCAL
#include <cstdio>
#include <cstdlib>
int findS(int *s, int v) {
while(s[v] >= 0) v = s[v];
return v;
}
void unionS(int *s, int c1, int c2) {
int r1 = findS(s, c1), r2 = findS(s, c2);
if(s[r1] < s[r2]) { s[r1] += s[r2]; s[r2] = r1; }
else { s[r2] += s[r1]; s[r1] = r2; }
}
int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif
int i, n, c1, c2, cnt = 0; char c;
scanf("%d\n%c", &n, &c);
int *s = (int*)malloc(sizeof(int)*(n+1));
for(i=0; i<=n; ++i) s[i] = -1;
while(c != 'S') {
scanf("%d%d", &c1, &c2);
if(c == 'I') unionS(s, c1, c2);
else if(c == 'C') {
if(findS(s, c1) == findS(s, c2)) printf("yes\n");
else printf("no\n");
}
scanf("\n%c", &c);
}
for(i=1; i<=n; ++i) if(s[i] < 0) ++cnt;
if(cnt == 1) printf("The network is connected.\n");
else printf("There are %d components.\n", cnt);
free(s);
return 0;
}