HDU 5438 拓扑排序+DFS
Ponds
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 3234 Accepted Submission(s): 997
Now Betty wants to remove some ponds because she does not have enough money. But each time when she removes a pond, she can only remove the ponds which are connected with less than two ponds, or the pond will explode.
Note that Betty should keep removing ponds until no more ponds can be removed. After that, please help her calculate the sum of the value for each connected component consisting of a odd number of ponds
For each test case, the first line contains two number separated by a blank. One is the number p(1≤p≤104) which represents the number of ponds she owns, and the other is the number m(1≤m≤105) which represents the number of pipes.
The next line contains p numbers v1,...,vp, where vi(1≤vi≤108) indicating the value of pond i.
Each of the last m lines contain two numbers a and b, which indicates that pond a and pond b are connected by a pipe.
7 7
1 2 3 4 5 6 7
1 4
1 5
4 5
2 3
2 6
3 6
2 7
#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include <vector>
using namespace std;
const int maxn = 1e4+;
vector<int> g[maxn];
int v[maxn];
int ans[maxn];
int vis[maxn];
int in[maxn]; int t;
int p,m;
int cnt;
void toposort()
{
queue<int> q;
for(int i = ; i<=p; i++)
if(in[i] <= ) //还有度数为0的点
q.push(i);
while(!q.empty())
{
int temp = q.front();
q.pop();
ans[temp]++;
for(int j = ; j<g[temp].size(); j++)
{
int vv = g[temp][j];
if(in[vv]<=) continue; //z这里有坑,否则就陷入死循环了。
in[vv]--;
if(in[vv] <= )
q.push(vv);
}
}
}
void dfs(int s,int& count,long long& sum)
{
if(ans[s]>||vis[s]) return;
vis[s] = ;
for(int i = ; i<g[s].size(); i++)
{
int vv = g[s][i];
if(ans[vv] == && !vis[vv])
{
count++;
sum += v[vv];
dfs(vv,count,sum);
}
}
}
int main()
{
scanf("%d",&t);
while(t--)
{
memset(ans,,sizeof(ans));
memset(in,,sizeof(in));
memset(vis,,sizeof(vis));
scanf("%d %d",&p,&m);
for(int i = ; i<=p; i++)
{
scanf("%d",&v[i]);
}
int l,r;
for(int i = ; i<=p; i++) g[i].clear();
for(int i = ; i<=m; i++)
{
scanf("%d %d",&l,&r);
g[l].push_back(r);
g[r].push_back(l);
in[l]++;
in[r]++;
}
toposort();
long long sum = ,sum1 = ;
int count = ;
for(int i = ; i<=p; i++)
{
if(ans[i] == &&!vis[i])
{
sum1 = v[i];
count = ;
dfs(i,count,sum1);
if(count% == ) sum += sum1;
}
}
printf("%I64d\n",sum); }
return ;
}
/*
312
7 10
1 20 300 400 500 1000 5000
1 2
1 3
1 4
2 3
2 4
3 4
5 6
6 7
5 7
3 6 2 1
10 20
1 2 3 2
10 100 1000
1 2
1 3 3 1
10 100 1000
1 2
*/
HDU 5438 拓扑排序+DFS的更多相关文章
- ACM/ICPC 之 拓扑排序+DFS(POJ1128(ZOJ1083)-POJ1270)
两道经典的同类型拓扑排序+DFS问题,第二题较第一题简单,其中的难点在于字典序输出+建立单向无环图,另外理解题意是最难的难点,没有之一... POJ1128(ZOJ1083)-Frame Stacki ...
- HDU 4857 拓扑排序 优先队列
n个数,已经有大小关系,现给m个约束,规定a在b之前,剩下的数要尽可能往前移.输出序列 大小关系显然使用拓扑结构,关键在于n个数本身就有大小关系,那么考虑反向建图,优先选择值最大的入度为零的点,这样得 ...
- HDU 1811 拓扑排序 并查集
有n个成绩,给出m个分数间的相对大小关系,问是否合法,矛盾,不完全,其中即矛盾即不完全输出矛盾的. 相对大小的关系可以看成是一个指向的条件,如此一来很容易想到拓扑模型进行拓扑排序,每次检查当前入度为0 ...
- 拓扑排序+DFS(POJ1270)
[日后练手](非解题) 拓扑排序+DFS(POJ1270) #include<stdio.h> #include<iostream> #include<cstdio> ...
- hdu 4324 拓扑排序
题意:给出一堆人的喜爱关系,判断有没有三角恋-_-|| 其实就是判断是否存在三条边的环. 一开始我是这么想的: 先拓扑排序,如果没有环那就直接No 如果有环?挑出环里的任意一个点(拓扑排序结束后不在拓 ...
- HDU 5638 拓扑排序+优先队列
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5638 题意: 给你一个DAG图,删除k条边,使得能个得到字典序尽可能小的拓扑排序 题解: 把拓扑排序 ...
- HDU 4324 (拓扑排序) Triangle LOVE
因为题目说了,两个人之间总有一个人喜欢另一个人,而且不会有两个人互相喜欢.所以只要所给的图中有一个环,那么一定存在一个三元环. 所以用拓扑排序判断一下图中是否有环就行了. #include <c ...
- 拓扑排序-DFS
拓扑排序的DFS算法 输入:一个有向图 输出:顶点的拓扑序列 具体流程: (1) 调用DFS算法计算每一个顶点v的遍历完成时间f[v] (2) 当一个顶点完成遍历时,将该顶点放到一个链表的最前面 (3 ...
- Ordering Tasks(拓扑排序+dfs)
Ordering Tasks John has n tasks to do. Unfortunately, the tasks are not independent and the executio ...
随机推荐
- hdu_2608_0 or 1_数论
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2608 反正我是没找出这个规律的,规律参考的别人的! /* 分析:假设数n=2^k*p1^s1*p2^s ...
- LeetCode OJ 222. Count Complete Tree Nodes
Total Accepted: 32628 Total Submissions: 129569 Difficulty: Medium Given a complete binary tree, cou ...
- Android Studio一直build、一直refreshing、一直buiding gradle project into的终极解决办法
打开我的电脑,打开C:\Users\用户名, 把红圈的文件夹都删了 如果AndroidStudioProjects文件夹里那些project都不重要,也可以跟红圈文件夹删了, 然后再打开android ...
- maven 国内镜像地址
由于连接国外网站时网速特慢,为解决这个问题,os china 建立了一个maven 的私服.为了记忆,特将此记录. settings.xml 设置镜像方法步骤如下: 1. mirrors 设置 < ...
- FreeRTOS中断优先级配置(重要)
FreeRTOS中断优先级配置(重要) 本章节为大家讲解FreeRTOS中断优先级配置,此章节非常重要,初学者经常在这里犯迷糊.对于初学者来说,本章节务必要整明白.12.1 NVIC基础知识12.2 ...
- Co-Debugging JNI with Android Studio and Visual Studio
Tutorials > Android > Integration with other tools > Co-Debugging JNI with Android Studio a ...
- Android中用友盟实现QQ的第三方登录
//首先应该去友盟的官网注册你的账号,创建一个应用,获得它的APPkey,也可以用它的API上的appkey,下载SDK,下面根据API文档一步步实现就行了. //下面是友盟的APi文档 1. 产品 ...
- hdu 1728 逃离迷宫 (BFS)
逃离迷宫 Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submissi ...
- 使用libvirt做适配的kvm虚拟机window server 2008 磁盘性能的提升
实验室自己做了一个iaas的项目,当时是为了更方面的在kvm和xen下进行迁移,所以选择了libvirt作为适配层. 昨天简单的测试一了一下我们跟qingcloud的性能对比.我们的linux主机性能 ...
- Android开机启动Activity或者Service方法(转载)
这段时间在做Android的基础开发,现在有一需求是开机启动,按照网上某些博文教程做了下,始终不成功,一开机总是提示所启动的应用程序意外终止,于是参考了Android SDK doc,终于解决问题,下 ...