Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:可恶的poj,卡stl有意思吗?

Posted by noigold at 2012-10-26 22:56:56 on Problem 2186
In Reply To:Re:可恶的poj,卡stl有意思吗? Posted by:lgeecn at 2012-09-15 22:57:04
LZ傻逼吧 我都过了 全STL的
#include<stack> 
#include<cstdio>
#include<vector> 
#include<iostream>
#include<algorithm> 
#define pb push_back 
#define rep(i,a,b) for (int i=a;i<=b;i++)
using namespace std;

const int MAX_N=10010; 

int n,m,t0=0,scc=0; 

bool in_stack[MAX_N]; 

int belong[MAX_N],d[MAX_N]; 

int low[MAX_N],dfn[MAX_N],Num[MAX_N]; 

vector<int> E[MAX_N],_E[MAX_N]; 

stack<int> Stack; 

void init(){
        ios::sync_with_stdio(false); 
        cin>>n>>m;
        rep(i,1,m){
                int a,b;
                cin>>a>>b;
                E[a].pb(b);
                _E[b].pb(a);  
        }
}
#define put_in_lab(x) Num[scc]++,belong[x]=scc,in_stack[x]=0;

void dfs(int vi){
        low[vi]=dfn[vi]=++t0;
        in_stack[vi]=1; 
        Stack.push(vi);
        rep(i,0,int(E[vi].size()-1)){
                int now=E[vi][i];
                if (!dfn[now]){
                        dfs(now);
                        low[vi]=min(low[vi],low[now]); 
                }
                else
                if (in_stack[now])
                        low[vi]=min(low[vi],dfn[now]); 
        }
        if (low[vi]==dfn[vi]){
                scc++; 
                put_in_lab(vi); 
                int h;  
                while ((h=Stack.top())!=vi){
                        put_in_lab(h);
                        Stack.pop();  
                }
                Stack.pop(); 
        }
}

void build_new_map(){
        rep(i,1,n)
                rep(j,0,int(_E[i].size()-1)){
                        int This=belong[_E[i][j]];
                        if (This==belong[i])
                                continue; 
                        d[This]++;
                } 
}

void solve(){
        rep(i,1,n)
                if (!dfn[i])
                        dfs(i); 
        build_new_map(); 
        int num=0,This;
        rep(i,1,scc)
                if (!d[i])
                        num++,This=i;
        if (num!=1)
                cout<<0<<endl;
        else
                cout<<Num[This]<<endl;
}

int main(){
        freopen("1.in","r",stdin);
        freopen("1.out","w",stdout); 
        
        init(); 
        solve(); 
        
        return 0;  
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator