codeforces 216 DIV C. Valera and Elections


C. Valera and Elections
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The city Valera lives in is going to hold elections to the city Parliament.

The city has n districts and n - 1 bidirectional roads. We know that from any district there is a path along the roads to any other district. Let's enumerate all districts in some way by integers from 1 to n, inclusive. Furthermore, for each road the residents decided if it is the problem road or not. A problem road is a road that needs to be repaired.

There are n candidates running the elections. Let's enumerate all candidates in some way by integers from 1 to n, inclusive. If the candidate number i will be elected in the city Parliament, he will perform exactly one promise — to repair all problem roads on the way from the i-th district to the district 1, where the city Parliament is located.

Help Valera and determine the subset of candidates such that if all candidates from the subset will be elected to the city Parliament, all problem roads in the city will be repaired. If there are several such subsets, you should choose the subset consisting of the minimum number of candidates.

Input

The first line contains a single integer n (2 ≤ n ≤ 105) — the number of districts in the city.

Then n - 1 lines follow. Each line contains the description of a city road as three positive integers xiyiti (1 ≤ xi, yi ≤ n1 ≤ ti ≤ 2) — the districts connected by the i-th bidirectional road and the road type. If ti equals to one, then the i-th road isn't the problem road; if tiequals to two, then the i-th road is the problem road.

It's guaranteed that the graph structure of the city is a tree.

Output

In the first line print a single non-negative number k — the minimum size of the required subset of candidates. Then on the second line print k space-separated integers a1, a2, ... ak — the numbers of the candidates that form the required subset. If there are multiple solutions, you are allowed to print any of them.

Sample test(s)
input
5
1 2 2
2 3 2
3 4 2
4 5 2
output
1
5 
input
5
1 2 1
2 3 2
2 4 1
4 5 1
output
1
3 
input
5
1 2 2
1 3 2
1 4 2
1 5 2
output
4
5 4 3 2 

这题用自己常用的map【】【】肯定不行啊,因为有10^5个点,10^5条边,其实以前月神就问过我碰到这样的情况怎么办,当时我不知道的说。他是指针控,这题他是用链表记录的,我和他不一样,我看我们数据结构书上说了稀疏矩阵的三元组表存储,所以这道我就用上了。

这题用dfs就行了,不断往下早,直到遍历完所有的点就行了,具体怎么做,还是看代码吧,我说不清楚。(自己的渣渣的表达能力实在是我自己都受不了自己)


代码:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<limits.h>
using namespace std;
struct node
{
	int be;
	int end;
	int v;
}list[211111];  //以三元组表的形式记录图 
int hash[211111];//记录以第i点为起点的边的数目 
int ans[211111];//记录改点是否需要输出 
int sum[211111];//以前i个点为起点的边的数目,方便查找 
int vis[211111];// 记录第i个点前面以vis[i]为起点道路的需要修理 
int hehe[211111];// 判断该点是否已经遍历了,如果已经遍历过,则不需要再遍历(因为图为n点,n-1条边) 
int cmp(node x,node y)
{
	return x.be < y.be;
}
void dfs(int t)
{
	if(hash[t]==0)
		return;
	for(int i= 1,j= sum[t-1]+1; i<= hash[t]; i++,j++)
	{
		if(hehe[list[j].end])
			continue;
		hehe[list[j].end]= hehe[list[j].be] +1;
		if(list[j].v==1)
		{
			ans[list[j].end]= 0;
			vis[list[j].end]= vis[list[j].be];
			dfs(list[j].end);
			// 如果该边不需要修理,则 记录之前需要修理的边		
		}
		else
		{
			ans[list[j].end]= 1;
			ans[list[j].be]= 0;
			ans[vis[list[j].be]]= 0;
			vis[list[j].be]= 0;
			vis[list[j].end]= list[j].end;
			dfs(list[j].end);
			// 如果该边需要修理,则list[j].end之前的边都会随之修理,
			// 所以 	ans[vis[list[j].be]]= 0; 代表将之前需要的边删除
			// 例如 4个点(编号1,2,3,4),点1和点2之间的边需要修理,23之间不需要,34之间需要
			// 遍历第一条边的时候,ans【2】= 1,vis【2】= 2;
			// 遍历第二条边的时候,vis【3】= vis【2】= 2; 表示前三个点中,点2之前有边需要修理, 
			// 那么遍历到第三条边的时候,ans【2】 应该变为0,
			//同时vis【4】= 4,表示点4之前有边需要修理,点2之前没有边需要修理
			 
		}
	}	
}
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		memset(ans,0,sizeof(ans));
		memset(vis,0,sizeof(vis));
		memset(hash,0,sizeof(hash));
		memset(sum,0,sizeof(sum));
		memset(hehe,0,sizeof(hehe));
		for(int i= 1; i< n; i++)
		{
			scanf("%d%d%d",&list[i].be,&list[i].end,&list[i].v);		
			hash[list[i].be]++;
			hash[list[i].end]++;
			list[n+i-1].be= list[i].end;
			list[n+i-1].end= list[i].be;
			list[n+i-1].v= list[i].v;//双向图,每条边记录两次 
		}
		sort(list+1,list+2*n-1,cmp);// 排序方便搜索的时候找到起点和终点	
		sum[0]= 0;
		for(int i=1; i<= n; i++)
			sum[i]=sum[i-1]+ hash[i]; //记录每个点的起点	
		hehe[1]= 1;		
		dfs(1);
		int anssum= 0;
		for(int i=1; i<= n; i++)
			if(ans[i])
				anssum++;
		printf("%d\n",anssum); 
		int flag= 0;
		for(int i= 1; i<= n; i++)
			if(ans[i])
			{
				if(flag==0)
				{
					printf("%d",i);
					flag= 1;
				}
				else
					printf(" %d",i);
			}
		if(anssum)			
			printf("\n");
	}
}



深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 1. **神经网络(Neural Networks)**:深度学习的基础是人工神经网络,它是由多个层组成的网络结构,包括输入层、隐藏层和输出层。每个层由多个神经元组成,神经元之间通过权重连接。 2. **前馈神经网络(Feedforward Neural Networks)**:这是最常见的神经网络类型,信息从输入层流向隐藏层,最终到达输出层。 3. **卷积神经网络(Convolutional Neural Networks, CNNs)**:这种网络特别适合处理具有网格结构的数据,如图像。它们使用卷积层来提取图像的特征。 4. **循环神经网络(Recurrent Neural Networks, RNNs)**:这种网络能够处理序列数据,如时间序列或自然语言,因为它们具有记忆功能,能够捕捉数据中的时间依赖性。 5. **长短期记忆网络(Long Short-Term Memory, LSTM)**:LSTM 是一种特殊的 RNN,它能够学习长期依赖关系,非常适合复杂的序列预测任务。 6. **生成对抗网络(Generative Adversarial Networks, GANs)**:由两个网络组成,一个生成器和一个判别器,它们相互竞争,生成器生成数据,判别器评估数据的真实性。 7. **深度学习框架**:如 TensorFlow、Keras、PyTorch 等,这些框架提供了构建、训练和部署深度学习模型的工具和库。 8. **激活函数(Activation Functions)**:如 ReLU、Sigmoid、Tanh 等,它们在神经网络中用于添加非线性,使得网络能够学习复杂的函数。 9. **损失函数(Loss Functions)**:用于评估模型的预测与真实值之间的差异,常见的损失函数包括均方误差(MSE)、交叉熵(Cross-Entropy)等。 10. **优化算法(Optimization Algorithms)**:如梯度下降(Gradient Descent)、随机梯度下降(SGD)、Adam 等,用于更新网络权重,以最小化损失函数。 11. **正则化(Regularization)**:技术如 Dropout、L1/L2 正则化等,用于防止模型过拟合。 12. **迁移学习(Transfer Learning)**:利用在一个任务上训练好的模型来提高另一个相关任务的性能。 深度学习在许多领域都取得了显著的成就,但它也面临着一些挑战,如对大量数据的依赖、模型的解释性差、计算资源消耗大等。研究人员正在不断探索新的方法来解决这些问题。
Codeforces Round 894 (Div. 3) 是一个Codeforces举办的比赛,是第894轮的Div. 3级别比赛。它包含了一系列题目,其中包括题目E. Kolya and Movie Theatre。 根据题目描述,E. Kolya and Movie Theatre问题要求我们给定两个字符串,通过三种操作来让字符串a等于字符串b。这三种操作分别为:交换a中相同位置的字符、交换a中对称位置的字符、交换b中对称位置的字符。我们需要先进行一次预处理,替换a中的字符,然后进行上述三种操作,最终得到a等于b的结果。我们需要计算预处理操作的次数。 根据引用的讨论,当且仅当b[i]==b[n-i-1]时,如果a[i]!=a[n-i-1],需要进行一次操作;否则不需要操作。所以我们可以遍历字符串b的前半部分,判断对应位置的字符是否与后半部分对称,并统计需要进行操作的次数。 以上就是Codeforces Round 894 (Div. 3)的简要说明和题目E. Kolya and Movie Theatre的要求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Codeforces Round #498 (Div. 3) (A+B+C+D+E+F)](https://blog.csdn.net/qq_46030630/article/details/108804114)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Codeforces Round 894 (Div. 3)A~E题解](https://blog.csdn.net/gyeolhada/article/details/132491891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值