3038(HDU)
来源:互联网 发布:ibm医疗大数据 编辑:程序博客网 时间:2024/06/14 20:12
#include<iostream>
#include<cstdio>
#include<string.h>
#include<cstring>
#include<string>
#include<stack>
#include<set>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#define LOCAL
#define ll long long
#define lll unsigned long long
#define MAX 1000009
#define eps 1e-8
#define INF 0x7fffffff
#define mod 1000000007
using namespace std;
/*
PS:http://blog.csdn.net/acm_cxlove/article/details/8086806
题意:
想法:反过来求,不是0是根节点!!
*/
int sum[MAX];//到根节点的距离
int father[MAX];
int n,m;
int find(int x)
{
if(father[x]!=x)
{
int t = father[x];
father[x] = find(t);
sum[x]+=sum[t];//回溯下
}
return father[x];
}
void init(int n)
{
for(int i = 0; i<=n; i++)
{
father[i] = i;
sum[i] = 0;
}
}
int main()
{
// freopen("date.txt","r",stdin);
int n,m;
int u,v,w;
int ans;
while(~scanf("%d%d",&n,&m))
{
ans = 0;
init(n);
for(int i = 0; i<m; i++)
{
scanf("%d%d%d",&u,&v,&w);
u--;
int xx = find(u);
int yy = find(v);
if(xx==yy)
{
if(sum[u] - sum[v]!=w)
{
ans++;
}
}
else
{
father[xx] = yy;
sum[xx] = sum[v] + w - sum[u];//xx到yy的距离,画图就知道了。
}
}
cout<<ans<<endl;
}
return 0;
}
#include<cstdio>
#include<string.h>
#include<cstring>
#include<string>
#include<stack>
#include<set>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#define LOCAL
#define ll long long
#define lll unsigned long long
#define MAX 1000009
#define eps 1e-8
#define INF 0x7fffffff
#define mod 1000000007
using namespace std;
/*
PS:http://blog.csdn.net/acm_cxlove/article/details/8086806
题意:
想法:反过来求,不是0是根节点!!
*/
int sum[MAX];//到根节点的距离
int father[MAX];
int n,m;
int find(int x)
{
if(father[x]!=x)
{
int t = father[x];
father[x] = find(t);
sum[x]+=sum[t];//回溯下
}
return father[x];
}
void init(int n)
{
for(int i = 0; i<=n; i++)
{
father[i] = i;
sum[i] = 0;
}
}
int main()
{
// freopen("date.txt","r",stdin);
int n,m;
int u,v,w;
int ans;
while(~scanf("%d%d",&n,&m))
{
ans = 0;
init(n);
for(int i = 0; i<m; i++)
{
scanf("%d%d%d",&u,&v,&w);
u--;
int xx = find(u);
int yy = find(v);
if(xx==yy)
{
if(sum[u] - sum[v]!=w)
{
ans++;
}
}
else
{
father[xx] = yy;
sum[xx] = sum[v] + w - sum[u];//xx到yy的距离,画图就知道了。
}
}
cout<<ans<<endl;
}
return 0;
}
0 0
- hdu 3038
- HDU 3038
- hdu 3038
- 3038(HDU)
- HDU 3038
- hdu 3038
- HDU 3038
- HDU---3038(并查集好题)
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- 发外链难还是建站难www.lanilani.cn
- 58 css 改变input type=file样式 placeholder属性
- 6.23 Block封装post异步网路请求& AFNetworking第三方封装
- nginx tomcat集群配置实现无痛重启服务教程python语言版本
- ZOJ_3795 Grouping(强连通分量 拓扑)
- 3038(HDU)
- Ubuntu下修改DNS方法(重启后也保留)
- System.Data.SQLite数据库介绍
- 求二叉树深度(递归方式)
- Log4j 2用法
- 10 个你需要了解的 Linux 网络和监控命令
- swift入门第二章运算符、字符串
- Ajax 完整教程
- 2-路归并排序