[dfs树 神奇思路] Codeforces 730K NEERC16 Southern Subregional K. Roads Orientation Problem

这种思路是怎么想到的 学习新姿势

https://wiki-new-meta.icpc-camp.org/2016-2017%20ACM-ICPC,%20NEERC,%20Southern%20Subregional%20Contest


#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<cstring>
#define cl(x) memset(x,0,sizeof(x))
using namespace std;
typedef pair<int,int> abcd;

inline char nc(){
  static char buf[100000],*p1=buf,*p2=buf;
  if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
  return *p1++;
}

inline void read(int &x){
  char c=nc(),b=1;
  for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
  for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

const int N=400005;
const int M=1000005;

struct edge{
  int u,v,next;
}G[M<<1];
int head[N],inum=1;

inline void add(int u,int v,int p){
  G[p].u=u; G[p].v=v; G[p].next=head[u]; head[u]=p;
}

int fat[N],depth[N],lnk[N];
#define V G[p].v
int fae[N],dir[M];
vector<int> back[N];

inline void dfs(int u,int fa,int fp){
  fat[u]=fa; depth[u]=depth[fa]+1;
  for (int p=head[u];p;p=G[p].next)
    if (p!=(fp^1)){
      if (!depth[V]){
	lnk[u]=V; fae[V]=(p>>1);
	dfs(V,u,p);
      }else if (depth[V]<depth[u]){
	back[lnk[V]].push_back(p>>1);
      }
    }
}

int n,m,s,t;
abcd Q[10000005]; int l,r;

inline void Clear(){
  for (int i=1;i<=n;i++) head[i]=0; inum=1;
  for (int i=1;i<=n;i++) fat[i]=depth[i]=lnk[i]=fae[i]=0,back[i].clear();
}

int main(){
  int T,iu,iv;
  freopen("t.in","r",stdin);
  freopen("t.out","w",stdout);
  read(T);
  while (T--){
    read(n); read(m); read(s); read(t);
    for (int i=1;i<=m;i++)
      read(iu),read(iv),add(iu,iv,++inum),add(iv,iu,++inum);
    for (int i=1;i<=m;i++) dir[i]=-1;
    dfs(s,0,0);
    if (!depth[t]){
      Clear(); printf("No\n"); continue;
    }
    l=r=-1;
    Q[++r]=abcd(t,0);
    while (l<r){
      int u=Q[++l].first,d=Q[l].second;
      while (fat[u] && dir[fae[u]]==-1){
	dir[fae[u]]=d;
	for (int j=0;j<(signed)back[u].size();j++){
	  dir[back[u][j]]=d;
	  int p=back[u][j]<<1;
	  if (G[p].v==fat[u]) Q[++r]=abcd(G[back[u][j]<<1].u,d^1);
	  if (G[p].u==fat[u]) Q[++r]=abcd(G[back[u][j]<<1].v,d^1);
	}
	u=fat[u];
      }
    }
    int flag=0;
    for (int i=1;i<=m;i++) if (dir[i]==-1) flag=1;
    if (flag){
      Clear(); printf("No\n"); continue;
    }
    printf("Yes\n");
    for (int i=1;i<=m;i++)
      if ((depth[G[i<<1].u]<depth[G[i<<1].v])==dir[i])
	printf("%d %d\n",G[i<<1].v,G[i<<1].u);
      else
	printf("%d %d\n",G[i<<1].u,G[i<<1].v);
    Clear();
  }
  return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值