标签:
2 4 6 1 2 2 4 1 3 1 2 2 2 2 3 4 6 1 2 2 3 1 4 2 1 2 1 2 1
1 2 2 1 4 4 1 1 3 4 4 4
/* ***********************************************
Author :CKboss
Created Time :2015年08月15日 星期六 12时15分02秒
File Name :HDOJ5385_2.cpp
************************************************ */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
const int maxn=100100;
struct P
{
int from,to,len;
}ep[maxn];
struct Edge
{
int to,next,id;
}edge[maxn];
int Adj[maxn],Size;
int n,m;
int Time[maxn];
bool vis[maxn];
void init()
{
memset(Time,0,sizeof(Time));
memset(vis,false,sizeof(vis));
memset(Adj,-1,sizeof(Adj));
Size=0;
}
void Add_Edge(int u,int v,int id)
{
edge[Size].to=v;
edge[Size].id=id;
edge[Size].next=Adj[u];
Adj[u]=Size++;
}
bool ans[maxn];
void color(int u,int t)
{
Time[u]=t;
for(int i=Adj[u];~i;i=edge[i].next)
{
int v=edge[i].to;
int eid=edge[i].id;
if(vis[v]==true) continue;
ans[eid]=true;
vis[v]=true;
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T_T;
scanf("%d",&T_T);
while(T_T--)
{
init();
scanf("%d%d",&n,&m);
for(int i=0,u,v;i<m;i++)
{
scanf("%d%d",&u,&v);
Add_Edge(u,v,i);
ep[i].from=u; ep[i].to=v; ep[i].len=-1;
}
int ti=1;
int s=1,e=n;
vis[1]=true; Time[1]=1;
memset(ans,false,sizeof(ans));
while(s<=e)
{
if(vis[s]) color(s++,ti++);
if(vis[e]) color(e--,ti++);
}
for(int i=0;i<m;i++)
{
if(ans[i]) printf("%d\n",abs(Time[ep[i].from]-Time[ep[i].to]));
else printf("%d\n",n);
}
}
return 0;
}
版权声明:----- from: http://blog.csdn.net/ck_boss
标签:
原文地址:http://blog.csdn.net/ck_boss/article/details/47681217