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

很奇怪的题,不考虑重边/考虑重边都会过,2 2 1 2 1 2 / 2 2 1 2 2 1都等于0也会过,附上代码

Posted by 2280103398 at 2014-02-25 22:17:12 on Problem 3177
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define INF 99999999
typedef long long LL;
using namespace std;

const int MAX=10000+10;
int n,m,size,top,index;
int uu[MAX],vv[MAX];
int head[MAX],dfn[MAX],low[MAX],stack[MAX],ind[MAX],oud[MAX],dp[MAX];

struct Edge{
	int v,next;
	Edge(){}
	Edge(int V,int NEXT):v(V),next(NEXT){}
}edge[MAX*2];

void Init(int num){
	for(int i=0;i<=num;++i)head[i]=-1,dfn[i]=ind[i]=oud[i]=0;
	size=top=index=0;
}

void InsertEdge(int u,int v){
	edge[size]=Edge(v,head[u]);
	head[u]=size++;
}

void tarjan(int u,int father){
	if(dfn[u])return;
	dfn[u]=low[u]=++index;
	stack[++top]=u;
	bool flag=true;
	for(int i=head[u];i != -1;i=edge[i].next){
		int v=edge[i].v;
		if(v == father/*flag*/){flag=false;continue;}
		tarjan(v,u);
		low[u]=min(low[u],low[v]);
	}
	if(dfn[u] == low[u]){
		while(stack[top] != u)low[stack[top--]]=low[u];
		--top;
	}
}

void dfs(int u,int father,int k){
	if(dfn[u])return;
	dfn[u]=1;
	dp[u]=k;
	for(int i=head[u];i != -1;i=edge[i].next){
		int v=edge[i].v;
		if(v == father)continue;
		++dp[u];
		dfs(v,u,1);
	}
}

int main(){
	while(~scanf("%d%d",&n,&m)){
		Init(n);
		for(int i=0;i<m;++i){
			scanf("%d%d",&uu[i],&vv[i]);
			InsertEdge(uu[i],vv[i]);
			InsertEdge(vv[i],uu[i]);
		}
		for(int i=1;i<=n;++i)tarjan(i,-1);
		for(int i=0;i<=n;++i)head[i]=-1,dfn[i]=0;
		size=0;
		for(int i=0;i<m;++i){//缩点重建图 
			if(low[uu[i]] == low[vv[i]])continue;
			InsertEdge(low[uu[i]],low[vv[i]]);
			InsertEdge(low[vv[i]],low[uu[i]]);
		}
		for(int i=1;i<=n;++i){
			dfs(low[i],-1,0);
		}
		int sum=0;
		for(int i=1;i<=n;++i){
			if(dp[low[i]] == 1)sum+=2-dp[low[i]],dp[low[i]]=3;
		}
		cout<<(sum+1)/2<<endl;
	}
	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