Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖
题目来源:Light OJ 1429 Assassin`s Creed (II)
题意:最少几个人走全然图 能够反复走 有向图
思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如今不是DAG 可能有环 而且每一个点可能反复走 对于有环 能够缩点 缩点之后的图是DAG图 另外点能够反复走和POJ 2594一样 先预处理连通性
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
const int maxn = 1010; int vis[maxn];
int y[maxn];
vector <int> G[maxn], G2[maxn], G3[maxn];
int n, m;
int a[maxn][maxn]; int pre[maxn];
int low[maxn];
int sccno[maxn];
int dfs_clock;
int scc_cnt;
stack <int> S; void dfs(int u)
{
pre[u] = low[u] = ++dfs_clock;
S.push(u);
for(int i = 0; i < G[u].size(); i++)
{
int v = G[u][i];
if(!pre[v])
{
dfs(v);
low[u] = min(low[u], low[v]);
}
else if(!sccno[v])
low[u] = min(low[u], pre[v]);
}
if(pre[u] == low[u])
{
scc_cnt++;
while(1)
{
int x = S.top();
S.pop();
sccno[x] = scc_cnt;
if(x == u)
break;
}
}
}
void find_scc()
{
dfs_clock = scc_cnt = 0;
memset(sccno, 0, sizeof(sccno));
memset(pre, 0, sizeof(pre));
for(int i = 1; i <= n; i++)
if(!pre[i])
dfs(i);
} void BFS(int u)
{
queue <int> Q;
memset(vis, 0, sizeof(vis));
vis[u] = true;
Q.push(u);
while(!Q.empty())
{
int x = Q.front(); Q.pop();
for(int i = 0; i < G2[x].size(); i++)
{
int v = G2[x][i];
if(vis[v])
continue;
vis[v] = true;
G[u].push_back(v);
Q.push(v);
}
}
}
bool dfs2(int u)
{
for(int i = 0; i < G3[u].size(); i++)
{
int v = G3[u][i];
if(vis[v])
continue;
vis[v] = true;
if(y[v] == -1 || dfs2(y[v]))
{
y[v] = u;
return true;
}
}
return false;
}
int match()
{
int ans = 0;
memset(y, -1, sizeof(y));
for(int i = 1; i <= scc_cnt; i++)
{
memset(vis, 0, sizeof(vis));
if(dfs2(i))
ans++;
}
return ans;
} int main()
{
int cas = 1;
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d %d", &n, &m);
for(int i = 0; i <= n; i++)
G[i].clear(), G2[i].clear(), G3[i].clear();
while(m--)
{
int u, v;
scanf("%d %d", &u, &v);
G2[u].push_back(v);
}
for(int i = 1; i <= n; i++)
BFS(i);
find_scc(); for(int u = 1; u <= n; u++)
{
for(int i = 0; i < G[u].size(); i++)
{
int v = G[u][i];
if(sccno[u] != sccno[v])
G3[sccno[u]].push_back(sccno[v]);
}
}
printf("Case %d: %d\n", cas++, scc_cnt-match());
}
return 0;
}
Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖的更多相关文章
- Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖
标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...
- Light OJ 1406 Assassin`s Creed 状态压缩DP+强连通缩点+最小路径覆盖
题目来源:Light OJ 1406 Assassin`s Creed 题意:有向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路:最少的的人能够走全然图 明显是最小路径覆盖问题 ...
- loj 1429(可相交的最小路径覆盖)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1429 思路:这道题还是比较麻烦的,对于求有向图的可相交的最小路径覆盖,首先要解决成环问 ...
- (最小路径覆盖) News 消息传递 (hust OJ 2604)
http://begin.lydsy.com/JudgeOnline/problem.php?id=2604 Description 总部最近打算向下面的N个工作人员发出了一条秘密消息.因为它是机 ...
- CodeForcesGym 100753B Bounty Hunter II 二分图最小路径覆盖
关键在建图 题解:http://www.cnblogs.com/crackpotisback/p/4856159.html 学习:http://www.cnblogs.com/jackiesteed/ ...
- Light OJ 1114 Easily Readable 字典树
题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 仅仅要满足每一个单词的首尾字符一样 中间顺序能够变化 思路:每一个单词除了首尾 中间的字符排序 ...
- Light OJ 1316 A Wedding Party 最短路+状态压缩DP
题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ...
- light oj 1007 Mathematically Hard (欧拉函数)
题目地址:light oj 1007 第一发欧拉函数. 欧拉函数重要性质: 设a为N的质因数.若(N % a == 0 && (N / a) % a == 0) 则有E(N)=E(N ...
- Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩
题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...
随机推荐
- java的new BufferedReader(new InputStreamReader(System.in))
流 JAVA /IO 基本小结 通过一行常见的代码讨论:new BufferedReader(new InputStreamReader(System.in)) /*** *** 看到这篇文章挺好的, ...
- [蘑菇街] 搜索、算法团队招募牛人啦-年底了走过路过不要错过 - V2EX
[蘑菇街] 搜索.算法团队招募牛人啦-年底了走过路过不要错过 - V2EX [蘑菇街] 搜索.算法团队招募牛人啦-年底了走过路过不要错过
- 三、nginx301跳转302跳转
301跳转设置: server { listen 80; server_name downcc.com; rewrite ^/(.*) http://www.downcc.com/$1 permane ...
- 08-IOSCore - App Store、国际化/本地化
App Store 1. 帐号身份 0. 有Xcode 写程序,在虚拟机上运行 1. 成为苹果使用者 appleid 验证邮箱 权限: 能下载应用程序 2. 成为苹果开发者 https://devel ...
- WebStorm 7.0 注册码
User Name: EMBRACE License Key:===== LICENSE BEGIN =====24718-1204201000001h6wzKLpfo3gmjJ8xoTPw5mQvY ...
- MinGW介绍与使用
3.1:MinGW 是什么? MinGW 提供了一套简单方便的Windows下的基于GCC 程序开发环境.MinGW 收集了一系列免费的Windows 使用的头文件和库文件:同时整合了GNU ( ht ...
- windows下apache如何完整卸载?
1.运行services.msc,在服务中停止 apache 服务.2.运行命令行程序,输入 sc delete apache,删除该服务3.删除apache文件夹.
- Swift - 使用NSURLSession同步获取数据(通过添加信号量)
过去通过 NSURLConnection.sendSynchronousRequest() 方法能同步请求数据.从iOS9起,苹果建议废除 NSURLConnection,使用 NSURLSessio ...
- Light OJ 1318 Strange Game 组合数+高速幂+分解因子
长度为l的用k种字符组成的字符串有k^l中 当中m个字符要不同样 那就是k^l*C(l, m)*(k-1)^m 有反复 要除以2 可是你mod n了 不能直接除 n不一定是素数 所以不能乘以逆元 所以 ...
- 一张图说清Asp.NET MVC中的 RenderPage、RenderBody、RenderSection