USACO Controlling Companies DFS

来源:互联网 发布:nvsip相似软件 编辑:程序博客网 时间:2024/05/16 01:01

参考: http://blog.sina.com.cn/s/blog_62213fa10100g4kx.html
我的代码:
1. 用stock[i][j]数组存储第i个公司对第j个公司掌控多少股份
2. m公司 原始的子公司c(第一级子公司),当发现stock[m][c] > 50时,要对c公司进行DFS,并且讲c公司控制的股票stock[c][i]加到stock[m][i]上;每一个拥有关系只能加一次,用visited数组进行记载

/* ID: wangxin12 PROG: concomLANG: C++ */ #include <iostream>#include <vector>#include <fstream>#include <string>using namespace std;#define COM 100int stock[COM + 1][COM + 1];  //i 公司 掌控 j公司多少股份bool visited[COM + 1][COM + 1];  //访问与否标记bool controls[COM + 1][COM + 1];  //i 公司 是否 控制 j公司int N;int from, to, share;void DFS(int mom, int child);int main() {ifstream fin("concom.in");ofstream fout("concom.out");fin>>N;int i, j, k;for(i = 0; i < N; i++) {fin>>from>>to>>share; stock[from][to] = share;}// first levelfor(i = 1; i <= COM; i++){for(j = 1; j<= COM; j++) {if( stock[i][j] > 50 && !visited[i][j] ) {controls[i][j] = true;DFS(i, j);visited[i][j] = true;}}}//outputfor(i = 1; i <= COM; i++)for(j = 1; j <= COM; j++)if(controls[i][j] && i != j) fout<<i<<" "<<j<<endl;fin.close();fout.close();return 0;}void DFS(int mom, int child) {if( visited[mom][child]) return;visited[mom][child] = true;for(int i = 1; i <= COM; i++) {stock[mom][i] += stock[child][i];if(stock[mom][i] > 50  ) {controls[mom][i] = true;DFS(mom, i);}}}


-------------------------------------------------------------------------------------------------------
题目:

Some companies are partial owners of other companies because they have acquired part of their total shares of stock. For example, Ford owns 12% of Mazda. It is said that a company A controls company B if at least one of the following conditions is satisfied:

  • Company A = Company B
  • Company A owns more than 50% of Company B
  • Company A controls K (K >= 1) companies denoted C1, ..., CK with each company Ci owning xi% of company B and x1 + .... + xK > 50%.

Given a list of triples (i,j,p) which denote company i owning p% of company j, calculate all the pairs (h,s) in which company h controls company s. There are at most 100 companies.

Write a program to read the list of triples (i,j,p) where i, j and p are positive integers all in the range (1..100) and find all the pairs (h,s) so that company h controls company s.

PROGRAM NAME: concom

INPUT FORMAT

Line 1:n, the number of input triples to followLine 2..n+1:Three integers per line as a triple (i,j,p) described above.

SAMPLE INPUT (file concom.in)

31 2 802 3 803 1 20

OUTPUT FORMAT

List 0 or more companies that control other companies. Each line contains two integers that denote that the company whose number is the first integer controls the company whose number is the second integer. Order the lines in ascending order of the first integer (and ascending order of the second integer to break ties). Do not print that a company controls itself.

SAMPLE OUTPUT (file concom.out)

1 21 32 3