#P1475. [USACO2.3] 控制公司 Controlling Companies

[USACO2.3] 控制公司 Controlling Companies

Description

Some companies are partial owners of other companies because they own a portion of the shares issued by those companies.

It is said that if at least one of the following three conditions is met, company AA can control company BB:

  • Company AA = company BB.
  • Company AA owns more than 50%50\% of company BB's stock.
  • Company AA controls KK (K1K \geq 1) companies, denoted C1,,CKC_1, \ldots, C_K, each company CiC_i owns xi%x_i\% of company BB's stock, and x1++xK>50%x_1+ \ldots + x_K \gt 50\%.

You are given a table where each row contains three numbers i,j,pi,j,p: it means company ii owns p%p\% of company jj. Compute all pairs (h,s)(h,s) indicating that company hh controls company ss. There are at most 100100 companies.

Input Format

The first line contains an integer NN, the number of rows in the table.

Each of the next NN lines contains three integers i,j,pi,j,p, meaning company ii owns p%p\% of company jj's stock.

Output Format

Output zero or more companies that control other companies. Each line contains two integers A,BA,B, meaning company AA controls company BB. Sort the output pairs in ascending order by AA as the primary key and BB as the secondary key.

Do not output cases where a company controls itself (i.e., all pairs must satisfy ABA \neq B).

3
1 2 80
2 3 80
3 1 20
1 2
1 3
2 3

Hint

Translated from NOCOW.

USACO 2.3.

Translated by ChatGPT 5