题目链接: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的更多相关文章

  1. ZOJ 1301 The New Villa (BFS + 状态压缩)

    题意:黑先生新买了一栋别墅,可是里面的电灯线路的连接是很混乱的(每个房间的开关可能控制其他房间,房间数<=10),有一天晚上他回家时发现所有的灯(除了他出发的房间)都是关闭的,而他想回卧室去休息 ...

  2. #292 (div.2) D.Drazil and Tiles (贪心+bfs)

    Description Drazil created a following problem about putting  ×  tiles into an n × m grid: "The ...

  3. ZOJ 1649:Rescue(BFS)

    Rescue Time Limit: 2 Seconds      Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put ...

  4. hdu-1728(贪心&&bfs的灵活运用吧)

    链接 [https://vjudge.net/contest/256476#problem/D] 题意 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到 ...

  5. bzoj 1193 贪心+bfs

    1193: [HNOI2006]马步距离 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2015  Solved: 914[Submit][Statu ...

  6. POJ - 2349 ZOJ - 1914 Arctic Network 贪心+Kru

    Arctic Network The Department of National Defence (DND) wishes to connect several northern outposts ...

  7. bzoj1193: [HNOI2006]马步距离(贪心+bfs)

    1193: [HNOI2006]马步距离 题目:传送门 题解: 毒瘤题... 模拟赛时的一道题,刚开始以为是一道大难题...一直在拼命找规律 结果.... 还是说正解吧: 暴力的解法肯定是直接bfs, ...

  8. 清北学堂-贪心-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 ...

  9. poj 1689 && zoj 1422 3002 Rubbery (Geometry + BFS)

    ZOJ :: Problems :: Show Problem 1689 -- 3002 Rubbery 这题是从校内oj的几何分类里面找到的. 题意不难,就是给出一个区域(L,W),这个区域里面有很 ...

随机推荐

  1. PL/SQL — 隐式游标

    一.隐式游标的定义及其属性 定义 隐式游标由系统自动定义,非显示定义游标的DML语句即被赋予隐式游标属性.其过程由oracle控制,完全自动化.隐式游标的名称是SQL,不能对SQL游标显式地执行OPE ...

  2. Servlet的getContextPath(), getServletPath(), getRequestURI(), getRealPath("/")

    假定web application 名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 则执行下面向行代码后打印出如下结果: ...

  3. CAShapeLayer--备用

    之前讲过CALayer动画相关知识,再来看看更加复杂的CAShapeLayer相关的动画知识. 普通CALayer在被初始化时是需要给一个frame值的,这个frame值一般都与给定view的boun ...

  4. bat写的自动部署脚本

    windows7的机器上重启服务需要关闭UAC ::编译部署项目 echo off echo 1. GatewayAdaptor echo 2. LogicService echo 3. Messag ...

  5. java.util.List

    /* * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETA ...

  6. oct(x) 将一个数字转化为8进制

    >>> a = 12 >>> b 21 >>> c = oct(a) >>> d = oct(b) >>> c ...

  7. sjtu1333 函数时代

    Description Taring说:生活的过程就是执行函数的过程.需要命令,也需要回报.更重要的,是得到回报的过程. 好吧...这道题其实和上面的也没啥关系TAT,我们要求的是下面的问题: 给定\ ...

  8. Codeforces Round #209 (Div. 2)

    A: 要么是两次要么4次,判断是否在边界: #include<cstdio> using namespace std; int main() { int n,m,x; ; scanf(&q ...

  9. 给自己加油,一定要学会MFC!

    我自己对于没有学会MFC始终耿耿于怀,都什么时代了啊,但是我仍然坚持会去学MFC,因为MFC虽然落后与复杂,但是在Windows平台上仍然是无所不能的(其实Windows平台仍然是唯一可以赚钱的平台, ...

  10. Spring事务传播特性的浅析——事务方法嵌套调用的迷茫

    Spring事务传播机制回顾 Spring事务一个被讹传很广说法是:一个事务方法不应该调用另一个事务方法,否则将产生两个事务.结果造成开发人员在设计事务方法时束手束脚,生怕一不小心就踩到地雷. 其实这 ...