第十四届浙江财经大学程序设计竞赛 J Journey【哈密欧拉顿回路】

链接:https://www.nowcoder.com/acm/contest/89/J
来源:牛客网

时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
Mr.Frog wants to travel all over the country, and as we all know the country consists of N cities and M directed roads connecting these cities. The cities are numbered from 1 to N. The roads are numbered from 1 to M, the ith road connects the city Ui to Vi which means Mr.Frog can move to city Vi from city Ui.

Mr.Frog wants to travel all cities, and he can choose a city to start his trip casually and choose a city to end his trip casually. In order to avoid the boredom in the journey, Mr.Frog decides to make a plan that can help him arrive each city exactly once and pass through each road exactly once. Now give you the information of the cities and roads. Please tell Mr.Frog whether there exists a way that can satisfy his plan.

输入描述:
The first line contains an integer T, where T is the number of test cases. T test cases follow.
For each test case, the first line contains two integers N and M, where N is the number of cities, and M is the number of roads.

Then next M lines follow, the ith line contains two integers Ui and Vi, indicating there is a directed road from city Ui to city Vi.

• 1 ≤ T ≤ 105.
• 1 ≤ N ≤ 105.
• 0 ≤ M ≤ 105.
• 1 ≤ Ui,Vi ≤ N.
• the sum of N in all test cases doesn’t exceed 106.• the sum of M in all test cases doesn’t exceed 106.

输出描述:
For each test case, print a line containing “Case #x:”, where x is the test case number (starting from 1).
In the next line, print the city number in the order of his journey, if there is a way that can satisfy his plan; otherwise print “NO”.
示例1
输入
2
4 3
1 2
2 3
3 4
4 2
1 2
3 4
输出
Case #1:
1 2 3 4
Case #2:
NO
备注:
For the first case, Mr.Frog chooses city 1 to start his trip and travels in the order of 1, 2, 3, 4.
For the second case, there is no way that can satisfy his plan, so print “NO”.

题意:给你一个图,要求你每个点每条边都只能经过一次的情况下遍历全图并打印。

分析:注意memset会超时,注意数据。实际上,这就是一条链!OJBK!

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<vector>
#include<math.h>
using namespace std;
#define ll long long int
#define INF 0x3f3f3f3f
const int maxn=1e5+10;
int a[maxn];

int du[maxn];
int n,m;
void init()
{
    memset(a,-1,sizeof a);

}
void init2()
{ 
    for(int i=1;i<=n;i++)
    {
        a[i]=-1;

        du[i]=0;
    }
}
void solve()
{
    if(n!=m+1)
    {
        printf("NO\n"); return;
    }
    int ft=0;
    for(int i=1;i<=n;i++)
    {
        if(du[i]>=2)
        {
            printf("NO\n"); return;
        }
        if(du[i]==0)
        {
            ft=i;break;
        }
    }
    int ans=1;
    if(ft==0)
    {
        printf("NO\n"); return;
    }
    for(int i=ft;a[i]!=-1;i=a[i])
    {
            ans++; 
            if(ans>n) break;
    }
    if(ans==n&&n==m+1)
    {
        printf("%d",ft);
        for(int i=ft;a[i]!=-1;i=a[i])
        {
            printf(" ");
            printf("%d",a[i]);
        }
        printf("\n");
    }
    else
    {
        printf("NO\n");
    }
}
int main()
{
    int cs=0;
    init();
    int T;
    scanf("%d",&T);
    while(T--)
    {
        cs++;
        int u,v;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&u,&v);
            a[u]=v;
            du[v]++;
        }
        printf("Case #%d:\n",cs);
        solve();
        init2();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值