[xsy 2672][构造]挑战NPC

题意
给你一个无向连通图,我们需要在这个图的基础上新建一个无向联通图。
该无向联通图的两个点i,j有一条边当且仅当它们在原图的最短距离不超过一个定值p。
输出哈密顿回路
n 1000 , n 1 m 10000 , p 3 n\leqslant 1000,n-1\leqslant m \leqslant 10000,p\geqslant 3
Solution
随便求出里面的一棵生成树,构造一下就行了。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m,p;
#define V 1005
#define E 20010
int head[V],v[E],nxt[E],tot=0;
bool vis[V];
inline void add_edge(int s,int e){
    tot++;v[tot]=e;nxt[tot]=head[s];head[s]=tot;
    tot++;v[tot]=s;nxt[tot]=head[e];head[e]=tot;
}
void dfs(int u,bool y){
    if(y)printf("%d ",u);
    vis[u]=true;
    for(int i=head[u];i;i=nxt[i])
        if(!vis[v[i]])dfs(v[i],!y);
    if(!y)printf("%d ",u);
}
int main(){
    scanf("%d%d%d",&n,&m,&p);
    int s,e;
    while(m--){
        scanf("%d%d",&s,&e);
        add_edge(s,e);
    }
    dfs(1,1);
    return 0;
}```

猜你喜欢

转载自blog.csdn.net/ezoilearner/article/details/84785049