ZOJ-2365 Strong Defence 贪心,BFS
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2365
我没看懂题目。。。这样理解:一个有向图,要给一些边染色,使得所用的颜色最多,且S到T的任意路径的都包含所有颜色。 或者: 给定一个无向图,图中有一个起点S和一个终点T。要求选K个集合S1,S2,…,SK,每个集合都含有图中的一些边,任意两个不同的集合的交集为空。并且从图中任意去掉一个集合,S到T都没有通路。要求K尽量大。
容易想到最短距离就是总共的数量tot,然后在图上求一个层次图,只要点的距离标号i<=tot的时候点标记为i就可以了,如果大于tot,任意1-tot都可以标记。证明比较简单,不多说。。
//STATUS:C++_AC_10MS_19492KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
//typedef __int64 LL;
//typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
//const LL LNF=1LL<<60;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End struct Edge{
int u,v,id;
}e[N*N];
int first[N],next[N*N];
int level[N],ans[N][],cnt[N],vise[N*N];
int Case,n,m,S,T,mt,tot; void adde(int a,int b,int c)
{
e[mt].u=a,e[mt].v=b,e[mt].id=c;
next[mt]=first[a],first[a]=mt++;
e[mt].u=b,e[mt].v=a,e[mt].id=c;
next[mt]=first[b],first[b]=mt++;
} int bfs(int flag)
{
int i,j,u,v,d;
queue<int> q;
q.push(S);
level[S]=;
while(!q.empty()){
u=q.front();q.pop();
for(i=first[u];i!=-;i=next[i]){
if(flag){
if(!vise[e[i].id]){
vise[e[i].id]=;
d=level[e[i].u]>tot?:level[u];
ans[d][cnt[d]++]=e[i].id;
}
}
// else if(e[i].v==T)return level[e[i].u];
if(!level[e[i].v]){
level[e[i].v]=level[u]+;
q.push(e[i].v);
}
}
}
return level[T]-;
} int main()
{
// freopen("in.txt","r",stdin);
int i,j,a,b;
scanf("%d",&Case);
while(Case--)
{
scanf("%d%d%d%d",&n,&m,&S,&T);
mem(first,-);mt=;
for(i=;i<=m;i++){
scanf("%d%d",&a,&b);
adde(a,b,i);
} mem(level,);
tot=bfs();
mem(level,);mem(cnt,);mem(vise,);
bfs(); printf("%d\n",tot);
for(i=;i<=tot;i++){
printf("%d",cnt[i]);
sort(ans[i],ans[i]+cnt[i]);
for(j=;j<cnt[i];j++){
printf(" %d",ans[i][j]);
}
putchar('\n');
}
}
return ;
}
ZOJ-2365 Strong Defence 贪心,BFS的更多相关文章
- ZOJ 1301 The New Villa (BFS + 状态压缩)
题意:黑先生新买了一栋别墅,可是里面的电灯线路的连接是很混乱的(每个房间的开关可能控制其他房间,房间数<=10),有一天晚上他回家时发现所有的灯(除了他出发的房间)都是关闭的,而他想回卧室去休息 ...
- #292 (div.2) D.Drazil and Tiles (贪心+bfs)
Description Drazil created a following problem about putting × tiles into an n × m grid: "The ...
- ZOJ 1649:Rescue(BFS)
Rescue Time Limit: 2 Seconds Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put ...
- hdu-1728(贪心&&bfs的灵活运用吧)
链接 [https://vjudge.net/contest/256476#problem/D] 题意 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到 ...
- bzoj 1193 贪心+bfs
1193: [HNOI2006]马步距离 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2015 Solved: 914[Submit][Statu ...
- POJ - 2349 ZOJ - 1914 Arctic Network 贪心+Kru
Arctic Network The Department of National Defence (DND) wishes to connect several northern outposts ...
- bzoj1193: [HNOI2006]马步距离(贪心+bfs)
1193: [HNOI2006]马步距离 题目:传送门 题解: 毒瘤题... 模拟赛时的一道题,刚开始以为是一道大难题...一直在拼命找规律 结果.... 还是说正解吧: 暴力的解法肯定是直接bfs, ...
- 清北学堂-贪心-bfs
输入样例: 3 5 10 5 4 10 8 1 10 1 3 1 4 1 5 1 3 2 1 2 5 4 3 4 3 4 5 5 1 1 4 4 6 1 9 4 7 2 9 5 10 5 2 8 8 ...
- poj 1689 && zoj 1422 3002 Rubbery (Geometry + BFS)
ZOJ :: Problems :: Show Problem 1689 -- 3002 Rubbery 这题是从校内oj的几何分类里面找到的. 题意不难,就是给出一个区域(L,W),这个区域里面有很 ...
随机推荐
- pycharm3.x 注册码
PyCharm 3.0 注册码 PyCharm3 序列号 License Key 用户名:yueting3527 注册码: ===== LICENSE BEGIN ===== 93347-120420 ...
- ASP.NET Mvc Razor视图语法
在ASP.NET MVC中有两套模版引擎,一套是ASPX,一套是Razor,从事过WebForms开发的朋友们,对于ASPX模版已经很熟悉了,下面我说一下我所熟悉的Razor模版引擎的一些语法,供大家 ...
- 创建第一个UI
创建一个2D UI 制作UI时,首先要创建UI的"根".在Unity顶部NGUI菜单中选择Create,然后选择2D UI. 创建完成后,在Scene窗口中,NGUI自动生成了一个 ...
- mysql 的存储引擎
注意:mysql开发很少显示使用外键,MyISAM可以定义外键,但不起作用. mysql最新的5.6版本支持,全文索引.
- C# - 接口,继承
接口 接口是把公共实例(非静态)方法和属性组合起来,以封装特定功能的一个集合.不能像实例化一个类那样实例化接口.接口不能包含实现其成员的任何代码,而只能定义成员本身.实现过程必须在实现接口的类中完成. ...
- CSS 背景颜色
颜色背景 CSS中背景颜色由background-color决定,这里的背景颜色会渲染padding和content,不会渲染border和margin部分. 在css3中可以通过background ...
- Oracle的登录操作
在完美的启动Oracle数据库之后就可以登录数据库了: 1. 首先登录时使用的用户名默认是“SYSTEM”密码是你安装的时候自行设置的. 登录使用的命令是“sqlplus / as sysdba”之后 ...
- [jobdu]第一个只出现一次的字符
刚开始还在想双指针,完全没必要.然后最土的可以扫两遍O(n),用一个数组记录出现次数.然后想到如果数组里记录出现的位置,可以只扫一遍O(n),第二遍扫字符集就行了. #include <iost ...
- 2、RenderScript的计算(2013.05.07)
一.官方文档翻译: Android Renderscript计算 参考网址: http://blog.csdn.net/fireofstar/article/details/7748143 http: ...
- hdu1838Chessboard(DP)
http://acm.hdu.edu.cn/showproblem.php?pid=1838 其实原先不知道这题是DP 我都想不到DP去 想了想没思路 看了下题解 经典思路 :第n大的都是由第n-1大 ...