hdu 2480 贪心+简单并查集
Steal the Treasure
Time Limit: 10000/6000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 775 Accepted Submission(s): 213

#include <iostream>
#include<cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn=+;
struct node
{
int x,y;
int d,w;
};
node edge[maxn*maxn/];
int fa[maxn];
int root(int x)
{
return (x==fa[x])?x:fa[x]=root(fa[x]);
}
bool cmp(node a,node b)
{
return a.w>b.w;
}
bool vis[maxn];
int n,m;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++)
fa[i]=i;
for(int i=;i<m;i++)
{
scanf("%d%d%d%d",&edge[i].x,&edge[i].y,&edge[i].d,&edge[i].w);
}
sort(edge,edge+m,cmp);
int ans=;
for(int i=;i<m;i++)
{
int fx=root(edge[i].x);
int fy=root(edge[i].y);
if(vis[fx]&&vis[fy]) continue;
if(edge[i].d==&&vis[fx]) continue;
ans+=edge[i].w;
if(edge[i].d==) vis[fx]=true;
else
{
if(fx==fy) vis[fx]=true;
else if(vis[fx]) vis[fy]=true;
else if(vis[fy]) vis[fx]=true;
else fa[fx]=fy;//缩点
}
}
printf("%d\n",ans);
}
}
hdu 2480 贪心+简单并查集的更多相关文章
- 1213 How Many Tables(简单并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单并查集,统计单独成树的数量. 代码: #include <stdio.h> #i ...
- POJ 2524 (简单并查集) Ubiquitous Religions
题意:有编号为1到n的学生,然后有m组调查,每组调查中有a和b,表示该两个学生有同样的宗教信仰,问最多有多少种不同的宗教信仰 简单并查集 //#define LOCAL #include <io ...
- poj1611 简单并查集
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 32781 Accepted: 15902 De ...
- 【简单并查集】Farm Irrigation
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
- Luogu 1525 【NOIP2010】关押罪犯 (贪心,并查集)
Luogu 1525 [NOIP2010]关押罪犯 (贪心,并查集) Description S城现有两座监狱,一共关押着N名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨 ...
- HDU 1811 拓扑排序 并查集
有n个成绩,给出m个分数间的相对大小关系,问是否合法,矛盾,不完全,其中即矛盾即不完全输出矛盾的. 相对大小的关系可以看成是一个指向的条件,如此一来很容易想到拓扑模型进行拓扑排序,每次检查当前入度为0 ...
- hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点)
hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点) 题意: 给一张无向连通图,有两种操作 1 u v 加一条边(u,v) 2 u v 计算u到v路径上桥的个数 ...
- ACM_“打老虎”的背后(简单并查集)
“打老虎”的背后 Time Limit: 2000/1000ms (Java/Others) Problem Description: “习大大”自担任国家主席以来大力反腐倡廉,各地打击贪腐力度也逐步 ...
- 简单并查集 -- HDU 1232 UVALA 3644 HDU 1856
并查集模板: #include<iostream> using namespace std; ],x,y; ]; //初始化 x 集合 void init(int n) { ; i< ...
随机推荐
- C语言:Message类
message.h #ifndef MESSAGE_H #define MESSAGE_H #define TRUE 1 #define FALSE 0 typedef struct { int se ...
- Red Hat TimesTen安装记录
1:内核参数修改 # vi /etc/sysctl.conf kernel.sem= #sysctl –p 备注:此安装过程为测试环境,具体参数修改要参考TimesTen官方文档. 2:创建用户及组信 ...
- poj 2492A Bug's Life
http://poj.org/problem?id=2492 #include<cstdio> #include<cstring> #include<algorithm& ...
- poj Building a Space Station
http://poj.org/problem?id=2031 #include<cstdio> #include<cstring> #include<cmath> ...
- COJ 1011 WZJ的数据结构(十一)树上k大
题解:主席树&DFS序. PS:为什么我一开始Wa了N发 是因为有一个左区间我写成[L,M+1]了.......................... #include<iostream ...
- Delphi7 THTTPRIO 控件设置超时
HTTPRIOLeExp.HTTPWebNode.SendTimeout := 100000; //发射 HTTPRIOLeExp.HTTPWebNode.ConnectTimeout := 10 ...
- A - ACM Computer Factory - poj 3436(最大流)
题意:有一个ACM工厂会生产一些电脑,在这个工厂里面有一些生产线,分别生产不同的零件,不过他们生产的电脑可能是一体机,所以只能一些零件加工后别的生产线才可以继续加工,比如产品A在生产线1号加工后继续前 ...
- config large memory
C Configuring Large Memory Optimization This appendix provides information for configuring memory op ...
- UVALive 4957 Fake scoreboard
题意就是有n个队伍和m个题目 给出了每个队伍解决的题目数量 每个题目也给出了被解决的次数 然后求一个方阵. N,Y表示每个队伍是否过了哪个题目. 要求字典序最小. 这题给人的第一反应就是网络流. 虽然 ...
- POJ3273:Monthly Expense(二分)
Description Farmer John is an astounding accounting wizard and has realized he might run out of mone ...