标签:des style http color io os ar java strong
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=3639
2 4 3 3 2 2 0 2 1 3 3 1 0 2 1 0 2
Case 1: 2 0 1 Case 2: 2 0 1 2
题目意思:
给一幅图,求点i,使得能够到达i的点数最多。
解题思路:
tarjan缩点,反向建图,然后dfs统计入度为0的联通分量,求出能够到达的节点数a,比较记录a最大的那些连通分量。
代码:
//#include<CSpreadSheet.h> #include<iostream> #include<cmath> #include<cstdio> #include<sstream> #include<cstdlib> #include<string> #include<string.h> #include<cstring> #include<algorithm> #include<vector> #include<map> #include<set> #include<stack> #include<list> #include<queue> #include<ctime> #include<bitset> #include<cmath> #define eps 1e-6 #define INF 0x3f3f3f3f #define PI acos(-1.0) #define ll __int64 #define LL long long #define lson l,m,(rt<<1) #define rson m+1,r,(rt<<1)|1 #define M 1000000007 //#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; #define Maxn 5500 int low[Maxn],dfn[Maxn],sta[Maxn],sc,bc,n,m,dep; bool iss[Maxn]; int in[Maxn],nu[Maxn],dei[Maxn],ans,temp; vector<vector<int> >myv; vector<vector<int> >tree; map<int,int>myp[Maxn]; bool vis[Maxn]; void tarjan(int cur) { int ne; low[cur]=dfn[cur]=++dep; sta[++sc]=cur; iss[cur]=true; for(int i=0;i<myv[cur].size();i++) { ne=myv[cur][i]; if(!dfn[ne]) { tarjan(ne); if(low[ne]<low[cur]) low[cur]=low[ne]; } else if(iss[ne]&&dfn[ne]<low[cur]) low[cur]=dfn[ne]; } if(low[cur]==dfn[cur]) { bc++; do { ne=sta[sc--]; iss[ne]=false; in[ne]=bc; nu[bc]++; }while(ne!=cur); } } void solve() { dep=sc=bc=0; memset(iss,false,sizeof(iss)); memset(dfn,0,sizeof(dfn)); memset(nu,0,sizeof(nu)); for(int i=0;i<n;i++) if(!dfn[i]) tarjan(i); } void dfs(int cur) { vis[cur]=true; temp+=nu[cur]; for(int j=0;j<tree[cur].size();j++) { int ne=tree[cur][j]; if(!vis[ne]) dfs(ne); } } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int t,kcas=0; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); myv.clear(); myv.resize(n+1); for(int i=1;i<=m;i++) { int a,b; scanf("%d%d",&a,&b); myv[a].push_back(b); } solve(); tree.clear(); tree.resize(bc+1); for(int i=1;i<=bc;i++) myp[i].clear(); memset(dei,0,sizeof(dei)); for(int i=0;i<n;i++) { for(int j=0;j<myv[i].size();j++) { int ne=myv[i][j]; if(in[ne]!=in[i]) { if(myp[in[ne]][in[i]]==0) { //system("pause"); myp[in[ne]][in[i]]=1; tree[in[ne]].push_back(in[i]); dei[in[i]]++; } } } } ans=0; /*printf("bc:%d\n",bc); for(int i=0;i<n;i++) printf("i:%d bei:%d dei:%d\n",i,in[i],dei[in[i]]);*/ for(int i=1;i<=bc;i++) { if(!dei[i]) { //printf("i:%d\n",i); //system("pause"); for(int j=1;j<=bc;j++) vis[j]=false; temp=0; dfs(i); nu[i]=temp; ans=max(ans,temp); } } printf("Case %d: %d\n",++kcas,ans-1); bool fi=true; for(int i=0;i<n;i++) { if(nu[in[i]]==ans) { if(!fi) putchar(' '); else fi=false; printf("%d",i); } } putchar('\n'); } return 0; } /* 4 4 0 1 0 2 1 3 2 3 */
[tarjan] hdu 3639 Hawk-and-Chicken
标签:des style http color io os ar java strong
原文地址:http://blog.csdn.net/cc_again/article/details/39717565