列表

详情


NC24747. [USACO 2010 Nov S]Chocolate Milk

描述

Farmer John's milk production and shipping system is an intricate one! He uses milking machines for his many cows to harvest the milk that then flows into pipes.
Each of these pipes connects a milking machine to a joint, where it might be joined by exactly one more pipe (the milk flowing through both pipes merges). The milk then flows through additional pipes (which all start and end at joints) until it reaches the long central pipe connecting to the distribution room.
The milk then goes through a reverse process of splitting at various joints until it is flows into milk tanks that are picked up and taken to market.
Farmer John notices that there is at most one way for milk to travel from one joint to any other joint. Furthermore, since Farmer John is an efficient man by nature, he has made sure that milk will flow through each and every pipe; in other words, no pipe is unneeded.
If we think of a milking machine, joint, or milk tank as a node, there are N (2 <= N <= 100,000) nodes in total (and N-1 pipes connecting them). The input describes each pipe as an ordered pair of nodes, A_i (1 <= A_i <= N) and B_i (1 <= B_i <= N; ) indicating milk flows from node A_i to node B_i. If there is no pipe coming in to A_i, it is a milking machine. Likewise, if no pipe goes out from B_i, it is a tank.
The demand of chocolate milk has skyrocketed in recent months, and Farmer John wants to install a chocolate inserter at one of the joints so he can create delicious chocolate milk for customers.
Being thrifty, Farmer John has only bought one chocolate inserter, so he wants to place it at a joint through which all the milk passes. He knows that such a joint exists.
Help Farmer John find all the possible places he can install the chocolate inserter. (Note that Farmer John cannot install the chocolate inserter at the same location as a milking machine.)
As an example, consider a milking setup like this one:            1 ----+                  |                  v            2 --> 4 --> 6 ------------------> 7 --> 8                        ^                     |                        |                     |            3 --> 5 ----+                     + --> 9 Visual inspection shows that the chocolate inserter can be installed at either joint 6 or 7, as all milk flows through those joints.

输入描述

* Line 1: A single integer: N
* Lines 2..N: Line i+1 contains two space-separated integers that describe a pipe's connectivity: A_i and B_i

输出描述

* Lines 1..??: Integers, one per line and in ascending order, each denoting a possible joint at which to install the chocolate inserter.

示例1

输入:

9 
1 4 
3 5 
2 4 
5 6 
6 7 
7 8 
4 6 
7 9 

输出:

6
7

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 43ms, 内存消耗: 2716K, 提交时间: 2019-10-19 12:16:23

#include<stdio.h>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
int n,rd[110000],cd[110000],yz[110000],xx,yy,to[110000],he,ta,q[110000],mx,vis[110000];
int main(){
	scanf("%d",&n);
	fo(i,2,n){
		scanf("%d%d",&xx,&yy);
		rd[yy]++;
		cd[xx]++;
		to[xx]=yy;
	}
	he=1;
	fo(i,1,n) if (!rd[i]){
		q[++ta]=i;
		yz[i]=1;
	}
	mx=1;
	while (he<=ta){
		if (cd[q[he]]==1){
			yz[to[q[he]]]+=yz[q[he]];
			vis[to[q[he]]]++;
			if (vis[to[q[he]]]==rd[to[q[he]]]){
				q[++ta]=to[q[he]];
				if (yz[to[q[he]]]>mx) mx=yz[to[q[he]]];
			}
		}
		he++;
	}
	fo(i,1,n) if (rd[i]&&mx==yz[i]) printf("%d\n",i);
	return 0;
}

C++(g++ 7.5.0) 解法, 执行用时: 35ms, 内存消耗: 1284K, 提交时间: 2022-09-12 19:20:34

#include<iostream>
#include<cstdio>
using namespace std;
const int N=1e5+7;
int nxt[N];
int rd[N],cd[N];
int n;
int main()
{
	scanf("%d",&n);
	int a,b;
	for(int i=1;i<=n-1;i++)
	{
		scanf("%d%d",&a,&b);
		nxt[a]=b;
		rd[b]++;
		cd[a]++;
	}
	int s=-1,t=-1;
	for(int i=1;i<=n;i++)
	{
		if(t==i&&(s<0||rd[i]>1)) s=i;
		if(cd[i]!=1) break;
		if(nxt[i]>t) t=nxt[i]; 
	}
	for(int i=s;i<=n;i=nxt[i])
	{
		printf("%d\n",i);
		if(cd[i]!=1) break;
	}
	return 0;
}

上一题