Copyright: Code original, For reprint, please send an email to [email protected] or [email protected], or qq3145743347 https://blog.csdn.net/ebirth/article/details/91354290.
Overview summary graph theory problem (2)
For graph theory, we respect the algorithm is more familiar with, this time, I found a common focus algorithm.
- Euler
Euler circuit is in a non-directed or directed graph problems obtaining a stroke in a specific painting .
method:
- To ponder whether this problem is solved by Euler tour.
- Thinking is a directed graph or not to apply to solve the Euler circuit diagram.
- For the FIG., The guarantee of a level of each point; FIG coefficient for, to ensure that each point is an even number.
For us this algorithm can be used to map:
on the way, Next, we look at the code:
title: http://120.77.248.79/problem/3023105
#include<bits/stdc++.h>
using namespace std;
const int maxn=200000;
const int maxm=200000;
struct node{
int v,next;
int len;
}e[maxm];
int g[maxn],ans;
void init(){
memset(g,-1,sizeof(g));
ans=0;
}
void add(int u,int v){
e[ans].v=v;
e[ans].next=g[u];
g[u]=ans++;
}
int n,m;
int degree[maxn];
int cnt;
bool vis[maxn];
void dfs(int u){
vis[u]=true;
cnt++;
for (int i=g[u];i!=-1;i=e[i].next){
int v=e[i].v;
if (!vis[v]){
dfs(v);
}
}
}
void euler(){
dfs(1);
if(cnt!=n){
cout<<0<<endl;
return;
}
int tot=0;
for(int i=1;i<=n;i++){
if(degree[i]%2==1){
tot++;
}
}
if(tot==0){
cout<<"1"<<endl;
}else{
cout<<"0"<<endl;
}
}
int main(){
init();
memset(degree,0,sizeof(degree));
cin>>n>>m;
for(int i=0;i<m;i++){
int u,v;
cin>>u>>v;
add(u,v);
add(v,u);
degree[u]++;
degree[v]++;
}
euler();
return 0;
}
This is the problem Euler approach, which is his basic thinking, then, we look at a question: https://www.luogu.org/problemnew/show/P2731
Code:
#include<bits/stdc++.h>
using namespace std;
int n;
int g[1010][1010];
int a[100000],b[100000];
int ans;
int s=1000000,q=1000000;
void add(int x){
for(int i=1;i<=500;i++){
if(g[x][i]){
g[x][i]--;
g[i][x]--;
add(i);
}
}
b[ans--]=x;
}
int main(){
cin>>n;
ans=n+1;
for(int i=1;i<=n;i++){
int u,v;
cin>>u>>v;
g[u][v]++;
g[v][u]++;
a[u]++;
a[v]++;
s=min(s,min(u,v));
}
for(int i=1;i<=500;i++){
if(a[i]%2==1){
q=min(i,q);
}
if(q<1000000){
add(q);
}else{
add(s);
}
}
for(int i=1;i<=n+1;i++){
cout<<b[i]<<endl;
}
return 0;
}
Similarly, in Euler's idea is the same.
See the next issue, chess (TOP sort)
this phase is completed, chess saying.