This is a explanation of this problem from USACO's training website. I have converted it to markdown. Please do not just copy code; you will not learn anything; at least type it out and understand so you can do it yourself in the future!

Some companies are partial owners of other companies because they have acquired part of their total shares of stock. For example, Ford at one point owned 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.

### INPUT FORMAT

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

### SAMPLE INPUT (file concom.in)

``````3
1 2 80
2 3 80
3 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 2
1 3
2 3
``````

Java

C++

Pascal

## ANALYSIS

Russ Cox

The method used here to solve the problem is as follows. We keep track of which companies control which other companies, and every time we hear that so and so owns this much percent of so and so, we update our information.

The array “owns” keeps track of how much of company j is owned by company i, whether directly or via controlled companies. The array “controls” keeps track of which companies are controlled by which other companies.

``````#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define NCOM 101

int owns[NCOM][NCOM];        /* [i,j]: how much of j do i and its
controlled companies own? */
int controls[NCOM][NCOM];    /* [i, j]: does i control j? */

/* update info: now i controls j */
void
{
int k;

if(controls[i][j])        /* already knew that */
return;

controls[i][j] = 1;

/* now that i controls j, add to i's holdings everything from j */
for(k=0; k<NCOM; k++)
owns[i][k] += owns[j][k];

/* record the fact that controllers of i now control j */
for(k=0; k<NCOM; k++)
if(controls[k][i])

/* if i now controls more companies, record that fact */
for(k=0; k<NCOM; k++)
if(owns[i][k] > 50)
}

/* update info: i owns p% of j */
void
addowner(int i, int j, int p)
{
int k;

/* add p% of j to each controller of i */
for(k=0; k<NCOM; k++)
if(controls[k][i])
owns[k][j] += p;

/* look for new controllers of j */
for(k=0; k<NCOM; k++)
if(owns[k][j] > 50)
}

void
main(void)
{
FILE *fin, *fout;
int i, j, n, a, b, p;

fin = fopen("concom.in", "r");
fout = fopen("concom.out", "w");
assert(fin != NULL && fout != NULL);

for(i=0; i<NCOM; i++)
controls[i][i] = 1;

fscanf(fin, "%d", &n);
for(i=0; i<n; i++) {
fscanf(fin, "%d %d %d", &a, &b, &p);