uva-11324-SCC+dp

https://vjudge.net/problem/UVA-11324

给出一幅有向图,问最大能找到多少个节点,使得这些节点中任意两个节点之间都至少有一条可达路径。

找出SCC后缩点求权重最大路即可。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<set>
#include<stack>
#include<deque>
#include<bitset>
#include<unordered_map>
#include<unordered_set>
#include<queue>
#include<cstdlib>
#include<ctype.h>
#include<ctime>
#include<functional>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define inf 0x3f3f3f3f
#define debug puts("debug")
#define mid ((L+R)>>1)
#define lc (id<<1)
#define rc (id<<1|1)
const int maxn=;
const int maxm=;
const double PI=acos(-1.0);
const double eps=1e-;
const LL mod=1e9+;
LL gcd(LL a,LL b){return b==?a:gcd(b,a%b);}
LL lcm(LL a,LL b){return a/gcd(a,b)*b;}
LL qpow(LL a,LL b,LL c){LL r=; for(;b;b>>=,a=a*a%c)if(b&)r=r*a%c;return r;}
template<class T>
void prt(T v){for(auto x:v)cout<<x<<' ';cout<<endl;}
struct Edge{int u,v,w,next;}; vector<int>g[maxn],g2[maxn];
int f[maxn],dfn[maxn],low[maxn],scc[maxn],tot[maxn],sum=,scc_cnt=;
stack<int>S;
void dfs(int u){
dfn[u]=low[u]=++sum;
S.push(u);
for(int v:g[u]){
if(!dfn[v]){
dfs(v),low[u]=min(low[u],low[v]);
}else if(!scc[v]){
low[u]=min(low[u],dfn[v]);
}
}
if(dfn[u]==low[u]){
++scc_cnt;
for(;;){
int x=S.top();S.pop();
scc[x]=scc_cnt;
tot[scc_cnt]++;
if(x==u)break;
}
}
}
int cal(int u){
if(f[u]!=-)return f[u];
f[u]=tot[u];
int maxx=;
for(int v:g2[u]){
maxx=max(maxx,cal(v));
}
return f[u]=f[u]+maxx;
}
int main(){
int t,n,m,i,j,u,v;
cin>>t;
while(t--){
scanf("%d%d",&n,&m);
for(i=;i<=n;++i)g[i].clear(),g2[i].clear(),tot[i]=low[i]=dfn[i]=scc[i]=;
sum=scc_cnt=;
while(!S.empty())S.pop();
while(m--){
scanf("%d%d",&u,&v);
g[u].pb(v);
}
for(i=;i<=n;++i){
if(!dfn[i]){
dfs(i);
}
}
memset(f,-,sizeof(f));
for(u=;u<=n;++u){
for(int v:g[u]){
if(scc[u]!=scc[v]){
//cout<<scc[v]<<' '<<scc[u]<<
g2[scc[v]].pb(scc[u]);
}
}
}
int ans=;
for(i=;i<=scc_cnt;++i){
f[i]=cal(i);
if(f[i]>ans)ans=f[i];
}
cout<<ans<<endl;
}
return ;
}
/*
1
5 5
1 2
2 3
3 1
4 1
5 2 */
上一篇:Go语言之高级篇beego框架之config、httplib、context


下一篇:mysql5.7安装