Given a directed graph G, consider the following transformation. First, create a new graphT(G) to have the same vertex set as G. Create a directed edge betweentwo vertices u and v in T(G) if and only if there is a pathbetween u and v in G that follows the directed edges only in the forwarddirection. This graph T(G) is often called the transitive closure of G.
We define a clique in a directed graph as a set of vertices U such thatfor any two vertices u and v in U, there is a directededge 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 integersn and m, where 0 ≤ n ≤ 1000 is the number ofvertices of Gand 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 u and vbetween 1 and n which definea 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).
1 5 5 1 2 2 3 3 1 4 1 5 2
4
给出有向图,求一个节点数最大的节点集,使节点集中任意两个点u和v满足:u可以到v或者v可以到u或者可以相互到达。
同一个强连通分量中的点要么都选,要么都不选,求出强连通分量并记录每个强连通分量的节点数,把这个强连通分量看成一个点,这个点的权值为强连通分量包含的节点数。缩点之后,把可达的点建边,这样形成了一个无环图,用记忆化搜索找一条权值最大的路径。
#include<iostream> #include<queue> #include<cstring> #include<cstdio> #include<cmath> #include<set> #include<map> #include<vector> #include<stack> #include<algorithm> #define INF 0x3f3f3f3f #define eps 1e-9 #define MAXN 1010 #define MAXM 2000010 #define MAXNODE 105 #define MOD 100000 #define SIGMA_SIZE 4 typedef long long LL; using namespace std; int T,N,M,pre[MAXN],low[MAXN],sccno[MAXN],dfs_clock,scc_cnt; int p[MAXN],dp[MAXN],vis[MAXN][MAXN]; stack<int> S; vector<int> G[MAXN],V[MAXN]; void dfs(int u){ pre[u]=low[u]=++dfs_clock; S.push(u); int L=G[u].size(); for(int i=0;i<L;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; p[scc_cnt]++; if(x==u) break; } } } void find_scc(){ dfs_clock=scc_cnt=0; memset(pre,0,sizeof(pre)); memset(sccno,0,sizeof(sccno)); memset(p,0,sizeof(p)); for(int i=1;i<=N;i++) if(!pre[i]) dfs(i); } int DP(int u){ int& ans=dp[u]; if(ans!=-1) return ans; ans=p[u]; int L=V[u].size(),MAX=0; for(int i=0;i<L;i++) MAX=max(MAX,DP(V[u][i])); ans+=MAX; return ans; } int main(){ freopen("in.txt","r",stdin); scanf("%d",&T); while(T--){ scanf("%d%d",&N,&M); for(int i=1;i<=N;i++) G[i].clear(); int u,v; while(M--){ scanf("%d%d",&u,&v); G[u].push_back(v); } find_scc(); for(int i=1;i<=scc_cnt;i++) V[i].clear(); memset(vis,0,sizeof(vis)); for(int u=1;u<=N;u++){ int L=G[u].size(); for(int i=0;i<L;i++){ int v=G[u][i],m=sccno[u],n=sccno[v]; if(m!=n&&!vis[m][n]){ V[m].push_back(n); vis[m][n]=1; } } } int ans=0; memset(dp,-1,sizeof(dp)); for(int i=1;i<=scc_cnt;i++) if(dp[i]==-1) ans=max(ans,DP(i)); printf("%d\n",ans); } return 0; }