1 条题解

  • 0
    @ 2025-2-14 21:30:06

    C++ :

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <string>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <set>
    #include <time.h>
    using namespace std;
    int path[2005],g[505][505],n,k;
    int maxn,minx;//求出最大和最小的点
    void dfs(int v)
    {
        int i;
        for(i=minx;i<=maxn;i++){
            if(g[v][i]){
                g[v][i]--;
                g[i][v]--;
                dfs(i);
            }
        }
        path[k++]=v;
    }
    int main()
    {
        int i;
        int a,b;
        while(cin>>n){
            memset(g,0,sizeof(g));
            memset(path,0,sizeof(path));
            minx=505;
            maxn=0;
            k=0;
            for(i=0;i<n;i++){
                cin>>a>>b;
                g[a][b]++;
                g[b][a]++;
                g[a][0]++;//a的度
                g[b][0]++;//b的度
                maxn=(a<b?b:a)<maxn?maxn:(a<b?b:a);
                minx=minx<(a<b?a:b)?minx:(a<b?a:b);
            }
            for(i=minx;i<=maxn;i++){
                if(g[i][0]%2){
                    dfs(i);
                    break;
                }
            }
            if(i==maxn+1){
                dfs(1);
            }
            for(int j=k-1;j>=0;j--)
                 printf("%d\n",path[j]);
        }
        return 0;
    }
    

    Pascal :

    var
    	n,i,j,s,t,m:word;
    	cir:boolean;
    	con:array[1..500,1..500]of word;
    	d:array[1..500]of word;
    	path:array[1..1025]of word;
    procedure search(s:word);
    var
      i:word;
    begin
      for i:=1 to m do
        if con[s,i]>0 then
        begin
          dec(con[s,i]);
    			dec(con[i,s]);
          search(i);
        end;
    	inc(j);
    	path[j]:=s;
    end;
    begin
    	readln(n);
    	for i:=1 to n do
    	begin
    		readln(s,t);
    		inc(con[s,t]);
    		inc(con[t,s]);
    		inc(d[s]);
    		inc(d[t]);
    		if s>m then m:=s;
    		if t>m then m:=t;
    	end;
    	cir:=true;
    	for i:=1 to m do
    		if odd(d[i]) then
    		begin
    			s:=i;
    			cir:=false;
    			break;
    		end;
    	j:=0;
    	if cir then search(1) else search(s);
    	for i:=n+1 downto 1 do writeln(path[i]);
    end.
    
    • 1

    信息

    ID
    965
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者