SPOJ 962 Intergalactic Map (网络流)

来源:互联网 发布:mac视频格式是什么 编辑:程序博客网 时间:2024/06/16 05:09

Description

Map Jedi knights, Qui-Gon Jinn and his young apprentice Obi-Wan Kenobi, are entrusted by Queen Padmé Amidala to saveNaboo from an invasion by the Trade Federation. They must leave Naboo immediately and go toTatooine to pick up the proof of the Federation’s evil design. They then must proceed on to the Republic’s capital planetCoruscant to produce it in front of the Republic’s Senate. To help them in this endeavor, the queen’s captain provides them with an intergalactic map. This map shows connections between planets not yet blockaded by the Trade Federation. Any pair of planets has at most one connection between them, and all the connections are two-way. To avoid detection by enemy spies, the knights must embark on this adventure without visiting any planet more than once. Can you help them by determining if such a path exists?

Note - In the attached map, the desired path is shown in bold.

注意:顶点编号可能不连续,甚至可能为负。

Input

The first line of the input is a positive integer t ≤ 20, which is the number of test cases. The descri_ptions of the test cases follow one after the other. The first line of each test case is a pair of positive integers n, m (separated by a single space). 2 ≤ n ≤ 30011 is the number of planets and m ≤ 50011 is the number of connections between planets. The planets are indexed with integers from 1 to n. The indices of Naboo, Tatooine and Coruscant are 1, 2, 3 respectively. The next m lines contain two integers each, giving pairs of planets that have a connection between them.

Output

The output should contain t lines. The ith line corresponds to the ith test case. The output for each test case should be YES if the required path exists andNO otherwise.

Sample Input

2
3 3
1 2
2 3
1 3
3 1
1 3

Sample Output

YES
NO

题意:在一个无向图中,一个人要从 1点赶往 2 点,之后再赶往 3 点,且要求中途不能多次经过同一个点。 问是否存在这样的路线。 (3 <= N <= 30011, 1 <= M <= 50011)。题目中提到的负顶点编号视为无效输入即可。

分析:从1到2再到3,按照这样的顺序建图不易,那么我们换个思维,从1到2再到3,和2到1,2到3是一样的道理,反正都是路线,哪个到哪个是无所谓的,那么我们就可以把2作为源点,加一个汇点只连接1和3,那么从源点到汇点跑出的最大流等于2代表存在这样的路线,由于每个点最多只能经过一次,那么显然我们要进行拆点,把i拆为 i 和 i ' ,那么建图:(i ,i ', 1),( i ' , i , 1)。

#include<stdio.h>#include<string.h>#include<vector>#include<queue>#include<algorithm>#include<sstream>#define maxn 70000#define LL long long#define inf 0x7fffffff#define INF 1e18using namespace std;struct edge{    int from,to,cap,flow;};vector<edge> edges;vector<int> G[maxn];int d[maxn],cur[maxn];bool vis[maxn];int s,t;void init(){    for(int i=0;i<maxn;i++) G[i].clear();    edges.clear();}void add(int from,int to,int cap){    edges.push_back((edge){from,to,cap,0});    edges.push_back((edge){to,from,0,0});    int m=edges.size();    G[from].push_back(m-2);    G[to].push_back(m-1);}bool bfs(){    memset(vis,false,sizeof(vis));    queue<int> q;    q.push(s);    d[s]=0,vis[s]=true;    while(!q.empty())    {        int x=q.front();        q.pop();        for(int i=0;i<G[x].size();i++)        {            edge& e=edges[G[x][i]];            if(!vis[e.to]&&e.cap>e.flow)            {                vis[e.to]=true;                d[e.to]=d[x]+1;                q.push(e.to);            }        }    }    return vis[t];}int dfs(int x,int a){    if(x==t||a==0) return a;    int flow=0,f;    for(int& i=cur[x];i<G[x].size();i++)    {        edge& e=edges[G[x][i]];        if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0)        {            e.flow+=f;            edges[G[x][i]^1].flow-=f;            flow+=f;            a-=f;            if(a==0) break;        }    }    return flow;}int maxflow(){    int flow=0;    while(bfs())    {        memset(cur,0,sizeof(cur));        flow+=dfs(s,inf);    }    return flow;}int main(){    int T;    scanf("%d",&T);    while(T--)    {        int n,m;        scanf("%d%d",&n,&m);        init();        s=0,t=2*n+1;        add(s,2+n,2);        add(1,t,1);        add(3,t,1);        for(int i=4;i<=n;i++)            add(i,i+n,1);        for(int i=1;i<=m;i++)        {            int x,y;            scanf("%d%d",&x,&y);            if(x<1||y<1||x>n||y>n) continue;            add(x+n,y,1);            add(y+n,x,1);        }        if(maxflow()==2) puts("YES");        else puts("NO");    }    return 0;}

0 0
原创粉丝点击