1 条题解
-
0
C :
#include<stdio.h> #include<stdlib.h> #define MAXVEX 1001 typedef int WeightType; typedef int Elemtype; typedef struct EdgeNode { int adjvex; WeightType weight; struct EdgeNode *next; }EdgeNode; typedef struct VexNode { EdgeNode *firstarc; }VexNode, AdjList[MAXVEX]; typedef struct GraphADJ{ AdjList adjlist; int numvex, numedge; }GraphADJ; void InitGraph(GraphADJ *G,int n) { int i; G->numvex = n; for(i=1; i<=n; i++) G->adjlist[i].firstarc = NULL; } void InsertEdge(GraphADJ *G,int s,int t,int w) { EdgeNode *newn = (EdgeNode *)malloc(sizeof(EdgeNode)); newn->adjvex = t; newn->weight = w; newn->next = G->adjlist[s].firstarc; G->adjlist[s].firstarc = newn; } int LocateEdge(GraphADJ G,int s,int t) { EdgeNode *p = G.adjlist[s].firstarc; while(p!=NULL) { if(p->adjvex == t) return 0; p = p->next; } return 1; } int Create(GraphADJ *G,int n,int m) { int s,t,i,x; int j=0; int a[1010],visit[2000]; InitGraph(G,n); for(i=1;i<=n;i++) visit[i]=0; for(i=0;i<m;i++) { scanf("%d%d",&s,&t); x=LocateEdge(*G,s,t); if(x==1) { InsertEdge(G,s,t,1); InsertEdge(G,t,s,1); } } EdgeNode *p; i=1; do { p=G->adjlist[i].firstarc; visit[i]=1; while(p!=NULL) { i=p->adjvex; if(visit[i]!=1) { a[j++]=i; visit[i]=1; } p=p->next; } j--; if(j>=0) i=a[j]; }while(j>=0); for(i=1;i<=n;i++) { if(visit[i]==1); else return 0; } return 1; } void main() { int n,m,x; while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0) break; GraphADJ G; x=Create(&G,n,m); if(x) printf("YES\n"); else printf("NO\n"); } }
C++ :
#include <stdio.h> int n,m; int a[1111][1111],b[1111],t; void dd(int x) { int i; b[x]=1; t++; for(i=1;i<=n;i++) if((a[x][i]==1)&&(b[i]==0)) dd(i); } int run() { int i,j,k; for(i=1;i<=n;i++) for(j=1;j<=n;j++) a[i][j]=0; for(i=1;i<=n;i++) b[i]=0; t=0; for(i=1;i<=m;i++) { scanf("%d%d",&j,&k); a[j][k]=1; a[k][j]=1; } dd(1); if(t<n) printf("NO\n"); else printf("YES\n"); } int main() { scanf("%d%d",&n,&m); while(n!=0) { run(); scanf("%d%d",&n,&m); } return 0; }
Pascal :
program acm1908; var g:array[1..1000,1..1000] of boolean; n,m,i,j,x,y:longint; vis:array[1..1000] of boolean; f:boolean; procedure dfs(v:longint); var i:longint; begin vis[v]:=true; for i:=1 to n do if (not vis[i]) and g[v,i] then dfs(i); end; begin readln(n,m); while (n<>0) or (m<>0) do begin fillchar(g,sizeof(g),0); fillchar(vis,sizeof(vis),0); for i:=1 to m do begin readln(x,y); g[x,y]:=true; g[y,x]:=true; end; dfs(1); f:=true; for i:=1 to n do if not vis[i] then begin writeln('NO'); f:=false; break;end; if f then writeln('YES'); readln(n,m); end; end.
- 1
信息
- ID
- 1108
- 时间
- 1000ms
- 内存
- 32MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者