The Largest Clique UVA - 11324( 强连通分量 + dp最长路)

这题 我刚开始想的是 缩点后 求出入度和出度为0 的点 然后统计个数 用总个数 减去
然而 这样是不可以的 画个图就明白了。。。

如果 减去度为0的点 那么最后如果出现这样的情况是不可以的
因为 1中的点 和 3 中的点不通。。
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define rap(a, n) for(int i=a; i<=n; i++)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
vector<int> G[];
int pre[maxn], low[maxn], sccno[maxn], dfs_clock, scc_cnt;
int w[maxn], line[maxn][maxn], d[maxn];
stack<int> s;
void dfs(int u)
{
pre[u] = low[u] = ++dfs_clock;
s.push(u);
for(int i=; 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(low[u] == pre[u])
{
scc_cnt++;
for(;;)
{
int x = s.top(); s.pop();
sccno[x] = scc_cnt;
if(x == u) break;
}
}
} void init()
{
dfs_clock = scc_cnt = ;
mem(sccno, );
mem(pre, );
mem(w, );
mem(d, -);
mem(line, );
for(int i=; i<maxn; i++) G[i].clear();
} int dp(int u)
{
int& ans = d[u];
if(ans >= ) return ans;
ans = w[u]; //最后一个点后边就没有点了
for(int i=; i<=scc_cnt; i++)
if(u != i && line[u][i])
ans = max(ans, dp(i) + w[u]);
return ans;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
init();
int n, m;
scanf("%d%d", &n, &m);
for(int i=; i<=m; i++)
{
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
}
for(int i=; i<=n; i++)
if(!pre[i])
dfs(i);
for(int i=; i<=n; i++)
{
w[sccno[i]]++; //统计每个强连通分量里的点的个数
for(int j=; j<G[i].size(); j++)
line[sccno[i]][sccno[G[i][j]]] = ;
}
int res = ;
for(int i=; i<=scc_cnt; i++) // 以每一个点为起点 去找最长路
res = max(res, dp(i)); printf("%d\n", res); } return ;
}
The Largest Clique UVA - 11324( 强连通分量 + dp最长路)的更多相关文章
- UVA 1324 The Largest Clique 最大团(强连通分量,变形)
题意:给一个有向图,要求找出一些点,使得这些点中的任意点对,要么可以互通,要么单向可达. 思路:最低只要求单向可达即可,即a->b都可以算进去. 强连通分量内的点肯定是满足要求的,可以全选,但是 ...
- Uva--11324--The Largest Clique【有向图强连通分量】
链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&am ...
- The Largest Clique UVA - 11324
题文:https://vjudge.net/problem/UVA-11324 题解: 这个题目首先可以发现,只要是一个强连通分量,要么都选,要么都不选,将点权看成强连通分量的点数,所以这个题目就转化 ...
- UVa 11324 The Largest Clique (强连通分量+DP)
题意:给定一个有向图,求一个最大的结点集,使得任意两个结点,要么 u 能到 v,要么 v 到u. 析:首先,如果是同一个连通分量,那么要么全选,要么全不选,然后我们就可以先把强连通分量先求出来,然后缩 ...
- UVA11324 The Largest Clique (强连通缩点+DP最长路)
<题目链接> 题目大意: 给你一张有向图 G,求一个结点数最大的结点集,使得该结点集中的任意两个结点 u 和 v 满足:要么 u 可以达 v,要么 v 可以达 u(u,v相互可达也行). ...
- ZOJ 3795 Grouping (强连通缩点+DP最长路)
<题目链接> 题目大意: n个人,m条关系,每条关系a >= b,说明a,b之间是可比较的,如果还有b >= c,则说明b,c之间,a,c之间都是可以比较的.问至少需要多少个集 ...
- [USACO2003][poj2138]Travel Games(dp/最长路)
http://poj.org/problem?id=2138 题意:给你一些单词和初始单词,在初始单词的任意位置你可以加任意一个字母,使得这个新单词在给的单词中有所出现,然后在这样不断迭代下去,让你求 ...
- Topcoder 12519 ScotlandYard(点对 dp+最长路)
题面传送门 题意: 有两个人 A 和 B 玩一个游戏.游戏规则大致是这样的: 有 \(n\) 个城市和三种交通工具公交.地铁和出租车. 给出三个 \(n\times n\) 的字符矩阵 \(b,m,t ...
- UVA 11324.The Largest Clique tarjan缩点+拓扑dp
题目链接:https://vjudge.net/problem/UVA-11324 题意:求一个有向图中结点数最大的结点集,使得该结点集中任意两个结点u和v满足:要目u可以到达v,要么v可以到达u(相 ...
随机推荐
- ptyhon基础篇 day1
1.变量 print('helloworld!') name = 'alex' name2 = 'jack' print(name,name2) 2.input #用户输入 username = in ...
- python 布尔值 bool( ) 与逻辑运算符
逻辑运算符 not and or 运算符优先级 not > and >or printer(x or y) x为非零,则返回x,否则返回y print(1 or 2) print(3 o ...
- Oracle数据库备份与还原命令
http://www.2cto.com/database/201305/210262.html
- linux菜鸟笔记
linux目录的子目录复制 cp -r 要复制的目录+新的目录 cp -r a test 意思就是将a的子目录及文件复制到新的目录test下面 zt@ubuntu:~/Desktop$ mkdir - ...
- google::protobuf 编译方法
这两天用了一下Protobuf 感觉很方便, 记录一下编译过程, 以做务忘(需要安装cmake): 1: 下载地址: https://developers.google.com/protocol-bu ...
- POJ 2251 Dungeon Master (三维BFS)
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
- Unity Lighting - Choosing a Color Space 选择色彩空间(四)
Choosing a Color Space 选择色彩空间 In addition to selecting a rendering path, it’s important to choose ...
- 如何停止AAD服务
Connect-MsolService (Get-MSOLCompanyInformation).DirectorySynchronizationEnabled 用这个命令查看是enable还是Dis ...
- python sys.argv是什么?
1.sys.argv 是获取运行python文件的时候命令行参数,且以list形式存储参数 2.sys.argv[0] 代表当前module的名字 下面的代码文件是a.py,当我不用IDE工具,只用命 ...
- tikv 安装
export HostIP="127.0.0.1" docker run -d -p 2379:2379 -p 2380:2380 --name pd pingcap/pd \ - ...