如果无向图 G 每对顶点 v 和 w 都有从 v 到 w 的路径,那么称无向图 G 是连通的。现在给定一张无向图,判断它是否是连通的。
输入格式
第一行有 2 个整数 n,m (0<n,m<106)。
接下来 m 行每行有 2 个整数 u,v (1≤u,v≤n) 表示 u 和 v 有边连接。
输出格式
如果无向图是连通的输出 yes,否则输出 no。
样例
input
4 6
1 2
2 3
1 3
4 1
2 4
4 3
output
yes
第一种dfs,运行错误,因为dfs次数太多栈溢出了。
注意结构体数组的在java中的使用
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
class Tree
{
public ArrayList<Integer>next = new ArrayList<Integer>();
}
public class Main {
final static double pi = Math.acos(-1);
static Tree tree[] = new Tree[1000001];
static int vis[] = new int [1000001];
public static void dfs(int now)
{
vis[now] = 1;
int l = tree[now].next.size();
for(int i=0;i<l;i++)
{
if(vis[tree[now].next.get(i)]==0)
dfs(tree[now].next.get(i));
}
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
int n = cin.nextInt();
int m = cin.nextInt();
for(int i=1;i<=n;i++)
tree[i] = new Tree();
for(int i=1;i<=m;i++)
{
int x = cin.nextInt();
int y = cin.nextInt();
tree[x].next.add(y);
tree[y].next.add(x);
}
dfs(1);
boolean seven = true;
for(int i=1;i<=n;i++)
{
if(vis[i]==0)
{
seven = false;
break;
}
}
if(seven)
System.out.println("yes");
else
System.out.println("no");
}
cin.close();
}
}
dfs栈溢出,于是改用bfs,这也是第一次在java中使用队列,然而又内存超限了。。。。。
注意java中队列的使用
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
class Tree
{
public ArrayList<Integer>next = new ArrayList<Integer>();
}
public class Main {
final static double pi = Math.acos(-1);
static Tree tree[] = new Tree[1000001];
static int vis[] = new int [1000001];
static Queue<Integer>queue = new LinkedList<Integer>();//注意队列的使用
public static void bfs(int now)
{
vis[now] = 1;
queue.add(1);
while(queue.isEmpty()==false)
{
int top = queue.poll();
int l = tree[top].next.size();
for(int i=0;i<l;i++)
{
int n = tree[top].next.get(i);
if(vis[n]==0)
{
vis[n] = 1;
queue.add(n);
}
}
}
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
int n = cin.nextInt();
int m = cin.nextInt();
for(int i=1;i<=n;i++)
tree[i] = new Tree();
for(int i=1;i<=m;i++)
{
int x = cin.nextInt();
int y = cin.nextInt();
tree[x].next.add(y);
tree[y].next.add(x);
}
bfs(1);
boolean seven = true;
for(int i=1;i<=n;i++)
{
if(vis[i]==0)
{
seven = false;
break;
}
}
if(seven)
System.out.println("yes");
else
System.out.println("no");
}
cin.close();
}
}
最后用了并查集,好久没写了,熟悉一下,终于过了,java确实不适用于竞赛。
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
final static double pi = Math.acos(-1);
public static int find(int father[],int now)//路径压缩
{
if(father[now]==now)
return now;
int fx = find(father,father[now]);
father[now] = fx;
return fx;
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
int n = cin.nextInt();
int m = cin.nextInt();
int vis[] = new int [n+1];
int father[] = new int [n+1];
for(int i=1;i<=n;i++)
father[i] = i;
for(int i=1;i<=m;i++)
{
int x = cin.nextInt();
int y = cin.nextInt();
int fx = find(father,x);
int fy = find(father,y);
father[fx] = fy;
}
boolean seven = true;
int p = find(father,1);
for(int i=1;i<=n;i++)
{
if(find(father,i)!=p)
{
seven = false;
break;
}
}
if(seven)
System.out.println("yes");
else
System.out.println("no");
}
cin.close();
}
}