Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
Problem B: The Largest Clique
Given a directed graph G, consider the following transformation. First, create a new graph T(G) to have the same vertex set as G. Create a directed edge between two vertices u and v in T(G) if and only if there is a path between u and v in G that follows the directed edges only in the forward direction. This graph T(G) is often called thetransitive closure of G.
We define a clique in a directed graph as a set of vertices U such that for any two vertices u and v in U, there is a directed edge either from u to v or from v to u (or both). The size of a clique is the number of vertices in the clique.
The number of cases is given on the first line of input. Each test case describes a graph G. It begins with a line of two integers n and m, where 0 ≤ n ≤ 1000 is the number of vertices of G and 0 ≤ m ≤ 50,000 is the number of directed edges of G. The vertices of G are numbered from 1 to n. The following m lines contain two distinct integers uand v between 1 and n which define a directed edge from u to v in G.
For each test case, output a single integer that is the size of the largest clique in T(G).
Sample input
1 5 5 1 2 2 3 3 1 4 1 5 2
Output for sample input
4
Zachary Friggstad
题意:求最大团
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<stack>
#include<algorithm>
#define N 1005
#define M 50005
using namespace std;
int n, m;
struct egde {
int v, nxt;
} e[M<<1]; //要进行重构图,可以将两个图的边写在一个数组里边。
int pos, head[2][N]; //两个图的head要分开。
void add(int u, int v, int f) {
e[pos].v = v;
e[pos].nxt = head[f][u];
head[f][u] = pos++;
}
int bcnt, belong[N], pre[N], cok;
stack<int>S;
int val[N]; //每个节点的权值;
int vis[N];
int dp[N]; //表示以x为节点开始的最大权值
int dfs(int u) {
int lowu = pre[u] = ++cok;
S.push(u);
for (int i = head[0][u]; i != -1; i = e[i].nxt) {
int v = e[i].v;
if (!pre[v]) {
int lowv = dfs(v);
lowu = min(lowu, lowv); //如果该点的子节点能达到更高的节点
} else if (!belong[v]) //只能通过没有确定编号的其他点
lowu = min(lowu, pre[v]);
}
if (lowu == pre[u]) { //如果u只能连回自己,那么u就是第一个被发现的节点
bcnt++;
while(1) {
int x=S.top();
S.pop();
belong[x] = bcnt;
if(x==u)break;
}
}
return lowu;
}
void tarjan(int n) {
cok = bcnt = 0;
memset(pre, 0, sizeof(pre));
memset(belong, 0, sizeof(belong));
for (int i = 1; i <= n; i++)
if (!pre[i])
dfs(i);
}
void rebuild(int n) {
memset(head[1], -1, sizeof(head[1]));
memset(val, 0, sizeof(val));
for (int u = 1; u <= n; u++) {
val[belong[u]]++;
for (int j = head[0][u]; ~j; j = e[j].nxt) {
int v = e[j].v;
if (belong[v] != belong[u])
add(belong[u], belong[v], 1); //没必要在此时判重:1.有向图有重边也没关系;2.待全部插入完毕后再判重效率会高很多;
}
}
//去重O(N^2)
for(int i=1;i<=bcnt;i++){
memset(vis,0,sizeof(vis));
int pre;//记录前面一个节点
for(int j=head[1][i];~j;j=e[j].nxt){
int v=e[j].v;
if(vis[v])e[pre].nxt=e[j].nxt;
else vis[v]=true;
pre=j;
}
}
}
int DP(int u) {
if (dp[u] != -1)
return dp[u];
int res = 0;
for (int i = head[1][u]; ~i; i = e[i].nxt) {
int v = e[i].v;
res = max(res, DP(v));
}
return dp[u] = res + val[u];
}
int main() {
int T;
//freopen("D:\\a.in","r",stdin);
scanf("%d", &T);
while (T--) {
scanf("%d%d", &n, &m);
memset(head[0], -1, sizeof(head[0]));
pos = 0;
while (m--) {
int u, v;
scanf("%d%d", &u, &v);
add(u, v, 0); //第一个图
}
tarjan(n);
rebuild(n);
memset(dp, -1, sizeof(dp));
int ans = 0;
for (int i = 1; i <= bcnt; i++) {
ans = max(dp[i]==-1?DP(i):dp[i], ans);
}
printf("%d\n", ans);
}
}