using namespace std;
bool visit[100005] = { false };
struct link {
int data;
struct link* next;
};
struct map {
int v, e;
link list[100005];
};
map create(int v, int e) {
map s = new map;
s->v = v;
s->e = e;
for (int i = 0; i < 100005; i++)
s->list[i].next = NULL;
for (int i = 0; i < e; i++) {
int start, end;
cin >> start >> end;
link* a = new link;
a->data = end;
a->next = s->list[start].next;
s->list[start].next = a;
}
return s;
}
void dfs(map s, int n, int& count) {
link temp;
count++;
visit[n] = true;
temp = s->list[n].next;
while (temp) {
if (!visit[temp->data])
dfs(s, temp->data, count);
temp = temp->next;
}
}
int main() {
int n, m, count = 0;
cin >> n >> m;
map* s = create(n, m);
dfs(s, 1, count);
if (count == n)
cout << “yes” << endl;
else
cout << “no” << endl;
return 0;
}
include
可能上述问题主要出在第一次我用了(u,v,w)写的,每次遍历边开销巨大,但第二次我用了邻接矩阵,直接编译错误了…
include
include
using namespace std;
define max_size 100010
int map[max_size][max_size];
bool visited[max_size];
int n,m;
queueq;
void bfs(int u0)
{
visited[u0]=true;
q.push(u0);
}
int main()
{
cin>>n>>m;
}
include
include
using namespace std;
vector > v;
vector visit;
int cnt = 0;
void dfs(int i) {
cnt++;
visit[i] = 1;
for (int j = 0;j < v[i].size();j++) {
if (visit[v[i][j]] == 0) dfs(v[i][j]);
}
}
int main() {
int n, m, a, b;
scanf(“%d%d”, &n, &m);
v.resize(n + 1);
visit.resize(n + 1);
for (int i = 0;i < m;i++) {
scanf(“%d%d”, &a, &b);
v[a].push_back(b);
v[b].push_back(a);
}
dfs(1);
if (cnt == n) printf(“yes”);
else printf(“no”);
return 0;
}
我用的dfs
define _CRT_SECURE_NO_WARNINGS
include
include
using namespace std;
bool visit[100005] = { false };
struct link {
int data;
struct link* next;
};
struct map {
int v, e;
link list[100005];
};
map create(int v, int e) {
map s = new map;
s->v = v;
s->e = e;
for (int i = 0; i < 100005; i++)
s->list[i].next = NULL;
for (int i = 0; i < e; i++) {
int start, end;
cin >> start >> end;
link* a = new link;
a->data = end;
a->next = s->list[start].next;
s->list[start].next = a;
}
return s;
}
void dfs(map s, int n, int& count) {
link temp;
count++;
visit[n] = true;
temp = s->list[n].next;
while (temp) {
if (!visit[temp->data])
dfs(s, temp->data, count);
temp = temp->next;
}
}
int main() {
int n, m, count = 0;
cin >> n >> m;
map* s = create(n, m);
dfs(s, 1, count);
if (count == n)
cout << “yes” << endl;
else
cout << “no” << endl;
return 0;
}
DFS一遍之后,遍历一遍访问数组,若结点都访问过则输出yes,存在结点没被访问则输出no。set用来保存输入的结点,采用邻接表存储(邻接矩阵不适合,太大了)
这道题是 dfs 判断连通的经典题。不过,因为它只让我们判断连通,所以其实还可以(~~水一把~~)直接用 Union_find。
我用bfs为什么通不过第二个测试点呃?难道是时间复杂度太高了?那应该用什么数据结构呢?
先排序,再遍历.可以不用BFS
include
using namespace std ;
struct pairs {
int a,b ;
};
bool comp (const pairs & c, const pairs & d);
pairs num [1000001] ;
bool ok [1000001] = {0} ;
int main (){
int n,m,i ;
cin >> n >> m;
int front,back ;
for (i =0;i > front >> back ;
num[i].a = (front > back) ? back : front;
num[i].b = (front > back) ? front :back ;
}
sort (num,num+m,comp) ;
if (num [0].a != 1){
cout << “no” << endl ;
return 0;
}
ok [num[0].a] = 1 ;
ok [num[0].b] = 1 ;
int cnt ;
if (num[0].a == num[0].b)
cnt = n -1 ;
else
cnt = n -2 ;
bool flag = 0;
for (i =1;i <n;i ++){
if (ok [num[i].a] == 1){
if (ok [num[i].b] == 0){
ok [num[i].b] = 1 ;
cnt – ;
}
}
else {
if (ok [num[i].b] == 1){
ok [num[i].a] = 1 ;
cnt – ;
}
else {
flag = 1;
break ;
}
}
}
if (flag || cnt )
cout << “no” << endl ;
else
cout << “yes” << endl ;
}
bool comp (const pairs & c, const pairs & d){
if (c.a == d.a)
return c.b < d.b ;
return c.a < d.a ;
}