//#define LOCAL#include<bits/stdc++.h>usingnamespace std;#define ll long long#define mem(a, b) memset(a,b,sizeof(a))#define sz(a) (int)a.size()#define INF 0x3f3f3f3f#define DNF 0x7f#define DBG printf("this is a input\n")#define fi first#define se second#define mk(a, b) make_pair(a,b)#define pb push_back#define LF putchar('\n')#define SP putchar(' ')#define p_queue priority_queue#define CLOSE ios::sync_with_stdio(0); cin.tie(0)template<typename T>voidread(T &x){x =0;char ch =getchar();ll f =1;while(!isdigit(ch)){if(ch =='-')f *=-1;ch =getchar();}while(isdigit(ch)){x = x *10+ ch -48; ch =getchar();}x *= f;}template<typename T,typename... Args>voidread(T &first, Args&... args){read(first);read(args...);}template<typename T>voidwrite(T arg){T x = arg;if(x <0){putchar('-'); x =- x;}if(x >9){write(x /10);}putchar(x %10+'0');}template<typename T,typename... Ts>voidwrite(T arg, Ts ... args){write(arg);if(sizeof...(args)!=0){putchar(' ');write(args ...);}}usingnamespace std;
ll gcd(ll a, ll b){return b ==0? a :gcd(b, a % b);}
ll lcm(ll a, ll b){return a /gcd(a, b)* b;}constint N =2e5+5;int n, m;int val[N], nval[N];int head[N], cnt;struct node {int t, next;}edge[N <<1];void add (int f,int t){
edge[cnt].t = t;
edge[cnt].next = head[f];
head[f]= cnt++;}int times , tot ;int dfn[N], low[N], vis[N], st[N], scc[N], type =0, ed[N];void tarjan (int u){
dfn[u]= low[u]=++ times;
st[++ tot]= u;
vis[u]=1;for(int i = head[u]; i !=-1; i = edge[i].next){int v = edge[i].t;if(!dfn[v]){tarjan(v);
low[u]= min (low[u], low[v]);}elseif(vis[v])
low[u]= min (low[u], dfn[v]);}if(low[u]== dfn[u]){
type ++;do{
scc[st[tot]]= type;
vis[st[tot]]=0;
tot --;}while(u != st[tot +1]);}return;}int in[N];
vector <int> eg[N];
queue <int> q;void bfs (int u){
q.push(u);
ed[u]=1;while(!q.empty()){int no = q.front();
q.pop();for(int i = head[no]; i !=-1; i = edge[i].next){int v = edge[i].t;if(!ed[v]){
ed[v]=1;
q.push(v);}}}}int main (void){
read (n, m);mem(head,-1);for(int i =1; i <= n ; i ++){
nval[i]= val[i]= INF;}for(int i =1; i <= m ; i ++){int x , v;
read (x, v);
val[x]=min(val[x], v);}
read (m);for(int i =1; i <= m ; i ++){int u , v;
read (u, v);
add (u, v);}for(int i =1; i <= n ; i ++){if(val[i]!= INF &&!ed[i])
bfs (i);}for(int i =1; i <= n ; i++){if(!dfn[i])tarjan(i);}for(int u =1; u <= n ; u ++){for(int j = head[u]; j !=-1; j = edge[j].next){int v = edge[j].t;if(scc[u]!= scc[v])
in[scc[v]]++;}}
ll sum =0;for(int i =1; i <= n ; i ++)
nval[scc[i]]= min (val[i], nval[scc[i]]);for(int i =1; i <= type ; i ++)if(!in[i])
sum += nval[i];int id =0;for(int i =1;i <= n ; i ++)if(!ed[i]){
id = i;break;}if(id){
cout <<"NO"<< endl;
cout << id << endl;}else{
cout <<"YES"<< endl;
cout << sum << endl;}}
P1407
强连通分量+建图
//#define LOCAL#include<bits/stdc++.h>usingnamespace std;#define ll long long#define mem(a, b) memset(a,b,sizeof(a))#define sz(a) (int)a.size()#define INF 0x3f3f3f3f#define DNF 0x7f#define DBG printf("this is a input\n")#define fi first#define se second#define mk(a, b) make_pair(a,b)#define pb push_back#define LF putchar('\n')#define SP putchar(' ')#define p_queue priority_queue#define CLOSE ios::sync_with_stdio(0); cin.tie(0)template<typename T>voidread(T &x){x =0;char ch =getchar();ll f =1;while(!isdigit(ch)){if(ch =='-')f *=-1;ch =getchar();}while(isdigit(ch)){x = x *10+ ch -48; ch =getchar();}x *= f;}template<typename T,typename... Args>voidread(T &first, Args&... args){read(first);read(args...);}template<typename T>voidwrite(T arg){T x = arg;if(x <0){putchar('-'); x =- x;}if(x >9){write(x /10);}putchar(x %10+'0');}template<typename T,typename... Ts>voidwrite(T arg, Ts ... args){write(arg);if(sizeof...(args)!=0){putchar(' ');write(args ...);}}usingnamespace std;
ll gcd(ll a, ll b){return b ==0? a :gcd(b, a % b);}
ll lcm(ll a, ll b){return a /gcd(a, b)* b;}constint N =2e5+5;int n, m;int head[N], cnt;struct node {int t, next;}edge[N <<1];void add (int f,int t){
edge[cnt].t = t;
edge[cnt].next = head[f];
head[f]= cnt++;}
string a , b;
map <string,int> ma;int times , tot ;int dfn[N], low[N], vis[N], st[N], scc[N], type =0;int buck[N];void tarjan (int u){
dfn[u]= low[u]=++ times;
st[++ tot]= u;
vis[u]=1;for(int i = head[u]; i !=-1; i = edge[i].next){int v = edge[i].t;if(!dfn[v]){tarjan(v);
low[u]= min (low[u], low[v]);}elseif(vis[v])
low[u]= min (low[u], dfn[v]);}if(low[u]== dfn[u]){
type ++;do{
scc[st[tot]]= type;
vis[st[tot]]=0;
tot --;}while(u != st[tot +1]);}return;}int main (void){
read (n);mem(head,-1);for(int i =1; i <= n ; i ++){
cin >> a >> b;if(!ma[a])
ma[a]=++tot;if(!ma[b])
ma[b]=++tot;
add (ma[a], ma[b]);//cout << ma[a] << ' ' << ma[b] << endl;}
read (m);for(int i =1; i <= m ; i ++){
cin >> a >> b;
add (ma[b], ma[a]);//cout << ma[b] << ' ' << ma[a] << endl;}for(int i =1; i <=2*n ; i +=2){if(!dfn[i])
tarjan (i);}for(int i =1; i <=2*n ; i +=2)
buck[scc[i]]++;for(int i =1; i <=2*n ; i +=2){if(buck[scc[i]]==1)
cout <<"Safe"<< endl;else
cout <<"Unsafe"<< endl;}}
[JSOI2010]连通数
反图+SCC+Topo+状态枚举
#include<bits/stdc++.h>usingnamespace std;#define ll long long#define mem(a, b) memset(a,b,sizeof(a))#define sz(a) (int)a.size()#define INF 0x3f3f3f3f#define DNF 0x7f#define DBG printf("this is a input\n")#define fi first#define se second#define mk(a, b) make_pair(a,b)#define pb push_back#define LF putchar('\n')#define SP putchar(' ')#define p_queue priority_queue#define CLOSE ios::sync_with_stdio(0); cin.tie(0)template<typename T>voidread(T &x){x =0;char ch =getchar();ll f =1;while(!isdigit(ch)){if(ch =='-')f *=-1;ch =getchar();}while(isdigit(ch)){x = x *10+ ch -48; ch =getchar();}x *= f;}template<typename T,typename... Args>voidread(T &first, Args&... args){read(first);read(args...);}template<typename T>voidwrite(T arg){T x = arg;if(x <0){putchar('-'); x =- x;}if(x >9){write(x /10);}putchar(x %10+'0');}template<typename T,typename... Ts>voidwrite(T arg, Ts ... args){write(arg);if(sizeof...(args)!=0){putchar(' ');write(args ...);}}usingnamespace std;
ll gcd(ll a, ll b){return b ==0? a :gcd(b, a % b);}
ll lcm(ll a, ll b){return a /gcd(a, b)* b;}constint N =3005;
map <pair <int,int>,int> ma;int n, m;int head[N], cnt;
bitset <N> bs[N];struct node {int t, next;}edge[N*N];void add (int f,int t){
edge[cnt].t = t;
edge[cnt].next = head[f];
head[f]= cnt++;}char ch[N];int times , tot , in[N];int dfn[N], low[N], vis[N], st[N], scc[N], type =0;void tarjan (int u){
dfn[u]= low[u]=++ times;
st[++ tot]= u;
vis[u]=1;for(int i = head[u]; i !=-1; i = edge[i].next){int v = edge[i].t;if(!dfn[v]){tarjan(v);
low[u]= min (low[u], low[v]);}elseif(vis[v])
low[u]= min (low[u], dfn[v]);}if(low[u]== dfn[u]){
type ++;do{
scc[st[tot]]= type;
vis[st[tot]]=0;
tot --;}while(u != st[tot +1]);
bs[type][type]=1;}return;}
vector <int> eg[N];int pp[N];voidtopo(){
queue <int> q;for(int i =1; i <= type ; i ++){if(!in[i])
q.push(i);}while(!q.empty()){int no = q.front();
q.pop();for(auto v : eg[no]){
bs[v]|= bs[no];
in[v]--;if(!in[v]){
q.push(v);}}}}int main (void){scanf("%d",&n);mem(head,-1);for(int i =1; i <= n ; i ++){scanf("%s", ch);for(int j =0; j < n ; j ++)if(ch[j]=='1'){add(j +1, i);}}for(int i =1; i <= n ; i ++)if(!dfn[i])tarjan(i);for(int u =1; u <= n ; u ++){for(int j = head[u]; j !=-1; j = edge[j].next){int v = edge[j].t;if(scc[u]!= scc[v])
eg[scc[u]].pb(scc[v]), in[scc[v]]++;}}for(int i =1; i <= n ; i ++)
pp[scc[i]]++;topo();
ll ans =0;for(int u =1; u <= type ; u ++){for(int v =1; v <= type ; v ++){if(bs[u][v])
ans += pp[u]* pp[v];}}printf("%lld\n",ans);}/*
5
01100
00101
00011
00000
00000
*/
[APIO2009]抢掠计划
SCC + DP + Topo
#include<bits/stdc++.h>usingnamespace std;#define ll long long#define mem(a, b) memset(a,b,sizeof(a))#define sz(a) (int)a.size()#define INF 0x3f3f3f3f#define DNF 0x7f#define DBG printf("this is a input\n")#define fi first#define se second#define mk(a, b) make_pair(a,b)#define pb push_back#define LF putchar('\n')#define SP putchar(' ')#define p_queue priority_queue#define CLOSE ios::sync_with_stdio(0); cin.tie(0)template<typename T>voidread(T &x){x =0;char ch =getchar();ll f =1;while(!isdigit(ch)){if(ch =='-')f *=-1;ch =getchar();}while(isdigit(ch)){x = x *10+ ch -48; ch =getchar();}x *= f;}template<typename T,typename... Args>voidread(T &first, Args&... args){read(first);read(args...);}template<typename T>voidwrite(T arg){T x = arg;if(x <0){putchar('-'); x =- x;}if(x >9){write(x /10);}putchar(x %10+'0');}template<typename T,typename... Ts>voidwrite(T arg, Ts ... args){write(arg);if(sizeof...(args)!=0){putchar(' ');write(args ...);}}usingnamespace std;
ll gcd(ll a, ll b){return b ==0? a :gcd(b, a % b);}
ll lcm(ll a, ll b){return a /gcd(a, b)* b;}constint N =1e6+5;int n, m, x, S, T[N];int head[N], cnt;struct node {int t, next;}edge[N];void add (int f,int t){
edge[cnt].t = t;
edge[cnt].next = head[f];
head[f]= cnt++;}char ch[N];int times , tot , in[N];
ll val[N], nval[N];int dfn[N], low[N], vis[N], st[N], scc[N], type =0;void tarjan (int u){
dfn[u]= low[u]=++ times;
st[++ tot]= u;
vis[u]=1;for(int i = head[u]; i !=-1; i = edge[i].next){int v = edge[i].t;if(!dfn[v]){tarjan(v);
low[u]= min (low[u], low[v]);}elseif(vis[v])
low[u]= min (low[u], dfn[v]);}if(low[u]== dfn[u]){
type ++;do{
scc[st[tot]]= type;
vis[st[tot]]=0;
tot --;}while(u != st[tot +1]);}return;}
vector <int> eg[N];
ll dp[N];voidtopo(){
queue <int> q;
q.push(scc[S]);
dp[scc[S]]= nval[scc[S]];while(!q.empty()){int u = q.front();
q.pop();for(auto v : eg[u]){
dp[v]=max(dp[v], dp[u]+ nval[v]);
in[v]--;if(!in[v])
q.push(v);}}}int main (void){
read (n, m);
mem (head,-1);for(int i =1; i <= m ; i ++){int u , v;
read (u , v);
add (u , v);}for(int i =1; i <= n ; i ++)
read (val[i]);
read (S, x);for(int i =1; i <= x ; i ++)
read (T[i]);tarjan(S);for(int u =1; u <= n ; u ++){for(int j = head[u]; j !=-1; j = edge[j].next){if(!dfn[u])continue;int v = edge[j].t;if(scc[u]!= scc[v]){
eg[scc[u]].pb(scc[v]), in[scc[v]]++;}}}for(int i =1; i <= n ; i ++)
nval[scc[i]]+= val[i];for(int u =1; u <= type ; u ++){if(scc[S]!= u &&!in[u]){for(auto v : eg[u])
in[v]--;}}
ll ans =0;topo();for(int i =1; i <= x ; i ++)
ans =max(ans, dp[scc[T[i]]]);write(ans), LF;}/*
5
01100
00101
00011
00000
00000
*/