Description
 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?
 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 <algorithm>
using namespace std;
const int maxn = 30010;
const int maxm = 100010;
const int inf = 0x3f3f3f3f;
struct G
{
    int v, cap, next;
    G() {}
    G(int v, int cap, int next) : v(v), cap(cap), next(next) {}
} E[maxm];
int p[maxn], T;
int d[maxn], temp_p[maxn], qw[maxn]; //d顶点到源点的距离标号,temp_p当前狐优化,qw队列
void init()
{
    memset(p, -1, sizeof(p));
    T = 0;
}
void add(int u, int v, int cap)
{
    E[T] = G(v, cap, p[u]);
    p[u] = T++;
    E[T] = G(u, 0, p[v]);
    p[v] = T++;
}
bool bfs(int st, int en, int n)
{
    int i, u, v, head, tail;
    for(i = 0; i <= n; i++) d[i] = -1;
    head = tail = 0;
    d[st] = 0;
    qw[tail] = st;
    while(head <= tail)
    {
        u = qw[head++];
        for(i = p[u]; i + 1; i = E[i].next)
        {
            v = E[i].v;
            if(d[v] == -1 && E[i].cap > 0)
            {
                d[v] = d[u] + 1;
                qw[++tail] = v;
            }
        }
    }
    return (d[en] != -1);
}
int dfs(int u, int en, int f)
{
    if(u == en || f == 0) return f;
    int flow = 0, temp;
    for(; temp_p[u] + 1; temp_p[u] = E[temp_p[u]].next)
    {
        G& e = E[temp_p[u]];
        if(d[u] + 1 == d[e.v])
        {
            temp = dfs(e.v, en, min(f, e.cap));
            if(temp > 0)
            {
                e.cap -= temp;
                E[temp_p[u] ^ 1].cap += temp;
                flow += temp;
                f -= temp;
                if(f == 0)  break;
            }
        }
    }
    return flow;
}
int dinic(int st, int en, int n)
{
    int i, ans = 0;
    while(bfs(st, en, n))
    {
        for(i = 0; i <= n; i++) temp_p[i] = p[i];
        ans += dfs(st, en, inf);
    }
    return ans;
}
int main(){
    int T, n, m;
    scanf("%d", &T);
    while(T--){
        init();
        scanf("%d %d", &n,&m);
        int st = 0, en = 2*n+1;
        add(st, n+2, 2);
        add(1, en, 1);
        add(3, en, 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);
        }
        int ans = dinic(st, en, en+1);
        if(ans == 2) puts("YES");
        else puts("NO");
    }
    return 0;
}
 
                   
                   
                   
                   
                             本文探讨了一个基于图论的问题,即如何在受限制的条件下寻找一条从起点到终点的有效路径。具体场景是在一个由星球组成的网络中,寻找一条不重复经过任一星球的路径。
本文探讨了一个基于图论的问题,即如何在受限制的条件下寻找一条从起点到终点的有效路径。具体场景是在一个由星球组成的网络中,寻找一条不重复经过任一星球的路径。
           
       
           
                 
                 
                 
                 
                 
                
               
                 
                 
                 
                 
                
               
                 
                 扫一扫
扫一扫
                     
              
             
                   1157
					1157
					
 被折叠的  条评论
		 为什么被折叠?
被折叠的  条评论
		 为什么被折叠?
		 
		  到【灌水乐园】发言
到【灌水乐园】发言                                
		 
		 
    
   
    
   
             
            


 
            