rwkj 1481

 

//1481
#include <iostream>
#include <cstdio>
using namespace std;
#define N 50010
int parent[N],sum;
void init()
{
for(int i=1;i<=N;i++)parent[i]=i;
}
int find(int x)
{
if(x==parent[x])return x;
else return find(parent[x]);
}
void marge(int a,int b)
{
int ta = find(a);
int tb = find(b);
if(ta!=tb) {parent[ta]=tb;sum--;}
}
int main()
{
int n,m,i,x,y,k;
while(scanf("%d%d",&n,&m)!=EOF&&(n+m))
{
init(); sum=n;
for(i=0;i<m;i++)
{
scanf("%d%d%d",&k,&x,&y);
if(x>n || y> n)
{sum--;continue;}
if(k==1) marge(x,y);
else if(k=2&&x==y)sum--;
}
printf("%d\n",n-sum);
}
return 0;
}

 

 

#include <iostream>
#include <cstdio>
using namespace std;
#define N 50010
int parent[N],sum;
void init()
{
for(int i=1;i<=N;i++)parent[i]=i;
}
int find(int x)
{
if(x==parent[x])return x;
else return parent[x]=find(parent[x]);
}
void marge(int a,int b)
{
int ta = find(a);
int tb = find(b);
if(ta!=tb) {parent[ta]=tb;sum--;}
}
int main()
{
int n,m,i,x,y,k;
while(scanf("%d%d",&n,&m)!=EOF&&(n+m))
{
init(); sum=n;
for(i=0;i<m;i++)
{
scanf("%d%d%d",&k,&x,&y);
if(x>n || y> n)
{sum--;continue;}
if(k==1) marge(x,y);
else if(k==2 && x==y )sum--;
}
printf("%d\n",n-sum);
}
return 0;
}

 

posted @   2014acm  阅读(139)  评论(0编辑  收藏  举报
编辑推荐:
· 后端思维之高并发处理方案
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
阅读排行:
· 后端思维之高并发处理方案
· 千万级大表的优化技巧
· 在 VS Code 中,一键安装 MCP Server!
· 想让你多爱自己一些的开源计时器
· 10年+ .NET Coder 心语 ── 继承的思维:从思维模式到架构设计的深度解析
点击右上角即可分享
微信分享提示