隐藏
「雅礼day8」图Graph - Dfs树 | Bill Yang's Blog

路终会有尽头,但视野总能看到更远的地方。

0%

「雅礼day8」图Graph - Dfs树

题目大意

给定一张 n 个点 m 条边的无向图,每条边连接两个顶点,保证无重边自环,不保证连通你想在这张图上进行若干次旅游,每次旅游可以任选一个点 x 作为起点,再走到一个与 x 直接有边相连的点 y,再走到一个与 y 直接有边相连的点 z 并结束本次旅游作为一个旅游爱好者,你不希望经过任意一条边超过一次,注意一条边不能即正向走一次又反向走一次,注意点可以经过多次,在满足此条件下,你希望进行尽可能多次的旅游,请计算出最多能进行的旅游次数并输出任意一种方案。


题目分析

完全同Codeforces round434 div2 F.Wizard’s Tour


代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
inline const int Get_Int() {
int num=0,bj=1;
char x=getchar();
while(x<'0'||x>'9') {
if(x=='-')bj=-1;
x=getchar();
}
while(x>='0'&&x<='9') {
num=num*10+x-'0';
x=getchar();
}
return num*bj;
}
const int maxn=200005;
int n,m,Depth[maxn],vst[maxn];
vector<int>edges[maxn];
void AddEdge(int x,int y) {
edges[x].push_back(y);
}
struct Answer {
int a,b,c;
Answer(int _a=0,int _b=0,int _c=0):a(_a),b(_b),c(_c) {}
};
vector<Answer>ans;
int Dfs(int Now,int father,int depth) {
Depth[Now]=depth;
vst[Now]=1;
vector<int>tmp;
for(auto &Next:edges[Now]) {
if(Next==father)continue;
if(vst[Next]) {
if(Depth[Next]<Depth[Now])tmp.push_back(Next);
continue;
}
int extra=Dfs(Next,Now,depth+1);
if(extra!=-1)ans.push_back(Answer(Now,Next,extra));
else tmp.push_back(Next);
}
for(int i=0; i+1<tmp.size(); i+=2)ans.push_back(Answer(tmp[i],Now,tmp[i+1]));
if(tmp.size()&1)return tmp.back();
else return -1;
}
int main() {
int size=40<<20;
__asm__ ("movq %0,%%rsp\n"::"r"((char*)malloc(size)+size));
n=Get_Int();
m=Get_Int();
for(int i=1; i<=m; i++) {
int x=Get_Int(),y=Get_Int();
AddEdge(x,y);
AddEdge(y,x);
}
for(int i=1; i<=n; i++)
if(!vst[i])Dfs(i,0,1);
printf("%d\n",ans.size());
for(auto &i:ans)printf("%d %d %d\n",i.a,i.b,i.c);
exit(0);
}
姥爷们赏瓶冰阔落吧~