#include <stdio.h>
#define DEBUG 1
#define TESTCASES 9
#define MAX_COMPANIES 100
int own[MAX_COMPANIES + 1][MAX_COMPANIES + 1];
int control[MAX_COMPANIES + 1][MAX_COMPANIES + 1];
int numOfTriples;
void addControl(int controller, int dominated){
if (control[controller][dominated])
return;
control[controller][dominated] = 1;
//先处理控制链的底端
int company;
for (company = 1; company <= MAX_COMPANIES; company++)
own[controller][company] += own[dominated][company];
for (company = 1; company <= MAX_COMPANIES; company++)
if (own[controller][company] > 50)
addControl(controller, company);
//再处理控制链的顶端
for (company = 1; company <= MAX_COMPANIES; company++)
if (control[company][controller])
addControl(company, dominated);
}
void addOwn(int owner, int belong, int percent){
int company;
for (company = 1; company <= MAX_COMPANIES; company++)
if (control[company][owner])
own[company][belong] += percent;
for (company = 1; company <= MAX_COMPANIES; company++)
if (own[company][belong] > 50)
addControl(company, belong);
}
int main(){
#if DEBUG
int testCase;
for (testCase = 1; testCase <= TESTCASES; testCase++){
char inputFileName[20] = "input3.txt";
inputFileName[5] = '1' + (testCase - 1);
freopen(inputFileName, "r", stdin);
printf("\n#%d\n", testCase);
#endif
int one, another;
for (one = 1; one <= MAX_COMPANIES; one++)
for (another = one; another <= MAX_COMPANIES; another++)
if (one == another){
control[one][another] = 1;
own[one][another] = 0;
} else {
control[one][another] = control[another][one] = 0;
own[one][another] = own[another][one] = 0;
}
scanf("%d", &numOfTriples);
int triple;
for (triple = 1; triple <= numOfTriples; triple++){
int owner, belong, percent;
scanf("%d%d%d", &owner, &belong, &percent);
addOwn(owner, belong, percent);
}
int controller, dominated;
for (controller = 1; controller <= MAX_COMPANIES; controller++)
for (dominated = 1; dominated <= MAX_COMPANIES; dominated++)
if (controller != dominated && control[controller][dominated])
printf("%d %d\n", controller, dominated);
#if DEBUG
}
#endif
return 0;
}
USACO 2.3 Controlling Companies (DFS)
最新推荐文章于 2021-08-17 15:47:47 发布