标签:强联通 cin 个数 c++ lse pre tle ace cpp
P2863 [USACO06JAN]牛的舞会The Cow Prom
用$tarjian$求出强连通分量,并且记录出强连通分量里的点的个数,如果个数大于$1$就$ans++$
#include<bits/stdc++.h>
#include<vector>
#include<stack>
using namespace std;
const int N=50010;
int dfn[N],low[N],sum,top,vis[N],col[N];
vector<int>g[N];
stack<int> q;
int num2;
int ans;
int tot[N];
int n,m,dep;
void tarjian(int u) {
++dep;
dfn[u]=low[u]=dep;
q.push(u);
vis[u]=1;
int len=g[u].size();
for(int i=0; i<len; ++i) {
int v=g[u][i];
if(!dfn[v]) {
tarjian(v);
low[u]=min(low[u],low[v]);
} else {
if(vis[u]) low[u]=min(low[u],low[v]);
}
}
if(dfn[u]==low[u])//找到强联通分量了
{
sum++;
num2++;
col[u]=sum;
vis[u]=0;
tot[num2]++;
while(q.top()!=u)
{
int Top=q.top();
q.pop();
col[Top]=sum;
tot[num2]++;
vis[Top]=0;
}
q.pop();
}
}
signed main() {
cin>>n>>m;
for(int i=1; i<=m; ++i) {
int x,y;
cin>>x>>y;
g[x].push_back(y);
}
for(int i=1; i<=n; ++i)
//if(!col[i]) tarjian(i);
if(!dfn[i]) tarjian(i);
for(int i=1;i<=num2;++i)
if(tot[i]>1) ans++;
cout<<ans;
return 0;
}
if(dfn[u]==low[u])//找到强联通分量了
{
sum++;
num2++;
col[u]=sum;
vis[u]=0;
tot[num2]++;//没有这一句
while(q.top()!=u)
{
int Top=q.top();
q.pop();
col[Top]=sum;
tot[num2]++;
vis[Top]=0;
}
q.pop();
}
少算了割点
P2863 [USACO06JAN]牛的舞会The Cow Prom
标签:强联通 cin 个数 c++ lse pre tle ace cpp
原文地址:https://www.cnblogs.com/pyyyyyy/p/11356931.html