poj 2230 欧拉回路
来源:互联网 发布:身体乳推荐知乎 编辑:程序博客网 时间:2024/06/05 14:16
题意,一个图,要将每条边恰好遍历两遍,而且要以不同的方向,还要回到原点。
直接dfs一下就好了,vis[]标记边是否访问,不会的仔细模拟一遍哪个dfs就好了
View Code
#include<stdio.h>
#include<string.h>
struct Edge{
int v,next;
}edge[111111];
int head[11111];
int n,m,tot;
bool vis[111111];
void add(int s,int t)
{
edge[tot].v=t;
edge[tot].next=head[s];
head[s]=tot++;
}
void dfs(int u)
{
for(int i=head[u];i!=-1;i=edge[i].next)
{
if(!vis[i])
{
vis[i]=true;
dfs(edge[i].v);
}
}
printf("%d\n",u);
}
int main()
{
scanf("%d%d",&n,&m);
int i,a,b;
tot=0;
memset(head,-1,sizeof(head));
for(i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
}
memset(vis,0,sizeof(vis));
dfs(1);
return 0;
}
java
View Code
import java.io.*;
import java.util.*;
import java.math.*;
class Edge{
int v,next;
boolean vis;
Edge (int v,int next){
this.v=v;
this.next=next;
this.vis=false;
}
}
class Graph{
static int MAXN = 10010;
static int n, m, size;
static int[] ans = new int[MAXN*3];
static int[] head = new int[MAXN];
static Edge[] edge = new Edge[MAXN*10];
Graph(){
Arrays.fill(head, -1);
size=0;
}
void add_edge(int u,int v){
edge[size]=new Edge(v,head[u]);
head[u]=size++;
}
void dfs(int u){
for(int i=head[u];i!=-1;i=edge[i].next){
if(!edge[i].vis){
edge[i].vis=true;
dfs(edge[i].v);
}
}
System.out.println(u);
}
}
public class Main{
public static void main(String[] args){
Scanner cin = new Scanner (System.in);
while(cin.hasNext()){
Graph a=new Graph();
a.n=cin.nextInt();
a.m=cin.nextInt();
for(int i=1;i<=a.m;i++){
int u=cin.nextInt();
int v=cin.nextInt();
a.add_edge(u, v);
a.add_edge(v, u);
}
a.dfs(1);
}
}
}
- POJ 2230 (欧拉回路)
- poj 2230 欧拉回路
- POJ 2230 欧拉回路
- POJ 2230 Watchcow 【欧拉回路】
- poj 2230 Watchcow (DFS + 欧拉回路)
- poj 2230 Watchcow(欧拉回路)
- poj - 2230 - Watchcow(欧拉回路)
- POJ 2230 欧拉回路变型
- POJ 2230 Watchcow 欧拉回路题解
- poj 2230 (简单欧拉回路)
- [欧拉回路] poj 2230 Watchcow
- POJ 2230 Watchcow(欧拉回路)
- POJ 2230 Watchcow (欧拉回路)
- poj 2230 Watchcow 欧拉回路
- POJ 2230 Watchcow (欧拉回路)
- POJ 2230 Watchcow( 欧拉回路 )
- POJ 2230 Watchcow (欧拉回路)
- Poj 2230 Watchcow【欧拉回路】
- poj 1283 DP
- 线段树 单点更新 【第一节】
- hdu 4152 dfs 枚举
- poj 2187 求平面最远点对 快速凸包 旋转卡壳
- poj 3982 高精度java
- poj 2230 欧拉回路
- poj 1840 哈希~~~
- ural 1088 二叉树基本性质
- 树状数组从前往后求和,用来解第k大(或小)的数 poj 2985 The k-th Largest Group
- poj 2985 线段树求第k大的数
- 线段树 树状数组 求大于某个值的第k小的数 hdu 2852 KiKi's K-Number
- 线段树【第二小节】
- Stanford Machine Learning -- 第四讲 Machine Learning System design
- hdu 3564 树状数组的灵活应用