定义
判别方法
P7771 【模板】欧拉路径(有向图)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f
#define re register
#define PII pair<int,int>
int read()
{
int f=1,k=0;char c = getchar();
while(c <'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9')k=(k<<1)+(k<<3)+(c^48),c=getchar();
return k*f;
}
const int N = 4e5+5;
int n,m;
vector<int> v[N];
int ing[N],deg[N];
int sta[N],top;
void dfs(int u)
{
while (v[u].size())
{
int j = v[u].back();
v[u].pop_back();//将该边删除
dfs(j); // 继续跑dfs
}
sta[++top] = u;
}
int main()
{
cin >> n >> m;
for (int i = 1;i <= m;i++)
{
int x = read(),y = read();
v[x].push_back(y);
ing[y]++,deg[x]++;
}
//先进行判定
int cnt1 = 0 ,cnt2 = 0,S = 0; // S 为起点
for (int i = 1;i <= n;i++)
{
sort(v[i].begin(),v[i].end(),greater<int>()); //先取尾所以倒序排序
if (ing[i] != deg[i])
{
if (abs(ing[i]-deg[i])>1)
{
puts("No");
return 0;
}
else if (ing[i] > deg[i]) cnt1++;
else cnt2++,S = i;
}
}
if (cnt1 == cnt2)
{
if (cnt1 == 0) S = 1;
dfs(S);
for (int i = top;i >= 1;i--) cout << sta[i] << ' ';
puts("");
}
else puts("No");
return 0;
}
P1341 无序字母对(无向图)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f
#define re register
#define PII pair<int,int>
int read()
{
int f=1,k=0;char c = getchar();
while(c <'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9')k=(k<<1)+(k<<3)+(c^48),c=getchar();
return k*f;
}
const int N = 2e5;
int n;
vector<PII > v[N];
int m;
bool vis[N];
int deg[N];
void add(int x,int y,int id)
{
v[x].push_back({y,id});
deg[y]++;
}
vector<int> ans;
void dfs(int u)
{
while (v[u].size())
{
auto j = v[u].back().first;
auto id = v[u].back().second;
v[u].pop_back();
if (!vis[id])
{
vis[id] = 1;
dfs(j);
}
}
ans.push_back(u);
}
int main()
{
cin >> m;
for (int i = 1;i <= m;i++)
{
char ch1,ch2;
cin >> ch1 >> ch2;
add(ch1,ch2,i);
add(ch2,ch1,i);
}
int cnt1 = 0,cnt2 = 0,S = INF;
int minS = INF;
for (int i = 0;i <= 200;i++) sort(v[i].begin(),v[i].end(),greater<PII>());
for (int i = 0;i <= 200;i++)
if (deg[i])
{
minS = min(minS,i);
if (deg[i] & 1) cnt1++,S = min(S,i);
else cnt2++;
}
if (cnt1==0 || cnt1 == 2)
{
if (!cnt1) S = minS;
dfs(S);
reverse(ans.begin(),ans.end());
for (auto it:ans) cout << (char)it ;
}
else puts("No Solution");
return 0;
}
标签:int,back,dfs,while,deg,回路,随笔,欧拉,define
From: https://www.cnblogs.com/codwarm/p/18316610