CSU 1601 War (并查集)

本文介绍了一道关于村庄间道路遭受攻击后的区域划分问题,通过使用并查集的数据结构来高效解决每次道路破坏后区域数量的变化。文章提供了详细的解题思路及完整的代码实现。

1601: War

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 202   Solved: 58
[ Submit][ Status][ Web Board]

Description

AME decided to destroy CH’s country. In CH’ country, There are N villages, which are numbered from 1 to N. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected. To defend the country from the attack of AME, CH has decided to build some roads between some villages. Let us say that two villages belong to the same garrison area if they are connected.
Now AME has already worked out the overall plan including which road and in which order would be attacked and destroyed. CH wants to know the number of garrison areas in his country after each of AME’s attack.

Input

The first line contains two integers N and M — the number of villages and roads, (2 ≤ N ≤ 100000; 1 ≤ M ≤ 100000). Each of the next M lines contains two different integers u, v (1<=u, v<=N)—which means there is a road between u and v. The next line contains an integer Q which denotes the quantity of roads AME wants to destroy (1 ≤ Q ≤ M). The last line contains a series of numbers each of which denoting a road as its order of appearance — different integers separated by spaces.

Output

Output Q integers — the number of garrison areas in CH’s country after each of AME's attack. Each pair of numbers are separated by a single space.

Sample Input

3 1
1 2
1
1
4 4
1 2
2 3
1 3
3 4
3
2 4 3

Sample Output

3
1 2 3

HINT

题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1601

题目大意:n个村子,m条路,依次给出连通两个村子的路,能相互连通的村子看做一个区域。再依次炸毁第 i 条路,求每次炸毁一条路后共有几个区域。

解题思路:典型的并查集,思维要反过来并,逆序把炸毁的路和并即可。时间是O(n),因此在合并每一条炸毁的路之前记录区域数目,注意如果两点已属于一块区域,则区域数目不变,如果不属于,则区域数目减1。

代码如下:

#include <cstdio>
#include <cstring>
const int maxn=100005;
int fa[maxn],ans[maxn],rd[maxn],vis[maxn];
struct  node
{
	int u,v;
}eg[maxn];
void get_fa()
{
 	for(int i=0;i<maxn;i++)
 		fa[i]=i;
}
int find(int x)
{
	return x==fa[x]?x:fa[x]=find(fa[x]);
}
void Union(int a,int b)
{
	int a1=find(a);
	int b1=find(b);
	fa[b1]=a1;
}
int main(void)
{
	int n,m,x;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		int q;
		get_fa();
		memset(vis,0,sizeof(vis));
		for(int i=1;i<=m;i++)
			scanf("%d%d",&eg[i].u,&eg[i].v);
		scanf("%d",&q);
		for(int i=1;i<=q;i++)
		{			
			scanf("%d",&rd[i]);
			vis[rd[i]]=1;
		}
		for(int i=1;i<=m;i++)
		{
			if(vis[i])continue;
			Union(eg[i].u,eg[i].v);
		}
		int a=0;
			for(int j=1;j<=n;j++)
				if(find(j)==j)
					a++;
		ans[q]=a;        //最开始的区域数目,即q条路被毁后的区域数目
		for(int i=q;i>1;i--)
		{
			if(find(eg[rd[i]].u)==find(eg[rd[i]].v))
			{
				ans[i-1]=ans[i];    //如果两个村子已经属于同一个区域,区域数目不变
				continue;
			}
			Union(eg[rd[i]].u,eg[rd[i]].v);   //如果不属于,合并,区域数目减1
			ans[i-1]=ans[i]-1;         //下标很重要,注意ans[i]表示第i条路炸毁后的区域数目,所以合并后记录的是上一条路炸毁后的情况
		}
		for(int i=1;i<=q;i++)
		{
			if(i<q)
				printf("%d ", ans[i]);
			else
				printf("%d\n",ans[i]);
		}
	}
}


下载代码方式:https://pan.quark.cn/s/604a73f2a5f9 流量分类机制(IEEE 802.1Qbv)将以太网数据传输划分为多个不同类别,每个类别均被分配特定时段以获取网络访问权,借此构建了类别专属的保护“路径”。依托IEEE 802.1Qcc的优化SRP与性能提升,用户网络接口(UNI)得到扩充,从而支持了远程集中化的网络设置。 ### IEEE 802.1Qbv TSN:流量调度技术详解 #### 一、IEEE 802.1Qbv TSN概述 在当前迅速演进的科技领域中,特别是工业自动化、汽车电子以及高性能计算等领域对实时通信的需求持续上升,时间敏感型网络(Time-Sensitive Networking, TSN)技术随之出现。其中,IEEE 802.1Qbv规范是TSN体系中的一个关键构成,主要聚焦于以太网中时间敏感数据流量的管理与调度。 #### 二、IEEE 802.1Qbv标准背景 IEEE 802.1Qbv由IEEE LAN/MAN标准委员会制定,作为IEEE 802.1Q-2014规范的一个延伸,目的是为支持定时传输的数据单元提供更高效、更精准的服务。该规范通过引入时间敏感的流量调度机制,使网络能更好地适应工业控制等环境下的实时性要求。 #### 三、核心概念阐释 **1. 流量调度(Scheduled Traffic)** - **定义**:IEEE 802.1Qbv的核心功能之一是流量调度,它允许依据预定的时间计划来传输不同类型的网络数据。 - **作用**:通过设定优先级和分配时间间隙,保障关键任务数据单元能在规定时限内完成传输,从而增强整个网络的可靠性与确定性。 **2. 类别特定的保护“路径”** - **...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值