1 条题解
-
0
C :
#include<stdio.h> int pre[100]; int path1[100],path2[100]; int main(){ int x,y,i,j,k; scanf("%d %d",&x,&y); int a,b; while(scanf("%d %d",&a,&b)==2){ pre[a]=b; } i=j=0; path1[i]=x; path2[j]=y; while(pre[x]!=0){ i++; path1[i]=pre[x]; x=pre[x]; } while(pre[y]!=0){ j++; path2[j]=pre[y]; y=pre[y]; } while(path1[i]==path2[j]){ i--; j--; } for(k=0;k<=i+1;k++){ printf("%d ",path1[k]); } for(k=j;k>=0;k--){ printf("%d ",path2[k]); } }
C++ :
#include<cstdio> #include<stack> using namespace std; const int N=100; int x,y; int f[N]; bool g[N]; stack<int>s; int main() { int a,b; scanf("%d%d",&x,&y); for(int i=1;i<N;i++) f[i]=i; while(scanf("%d%d",&a,&b)!=EOF&&a) f[a]=b; if(x==y) printf("%d",x); else { a=x,b=y; g[x]=true; while(x!=f[x]) x=f[x],g[x]=true; while(g[y]!=true) y=f[y],s.push(y); g[y]=false; while(g[a]==true) printf("%d ",a),a=f[a]; while(!s.empty()) printf("%d ",s.top()),s.pop(); printf("%d",b); } return 0; }
Pascal :
program qi26885; var x,y,m,n,root:longint; father,a:array[1..100]of longint; procedure du; begin readln(x,y); while not eof do begin readln(m,n); father[m]:=n; end; end; procedure find; var i,j:longint; begin i:=x; while i<>0 do begin a[i]:=1; i:=father[i] end; j:=y; while a[j]<>1 do j:=father[j]; root:=j; end; procedure finda(i:longint); begin if i<>root then begin write(i,' '); finda(father[i]); end; end; procedure findb(i:longint); begin if i<>root then begin findb(father[i]); if i<>y then write(i,' ') else writeln(i); end; end; begin du; find; finda(x); write(root,' '); findb(y); end.
- 1
信息
- ID
- 3561
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者