답안 #286544

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
286544 2020-08-30T14:23:42 Z gs18115 Travelling Salesperson (CCO20_day2problem1) C++14
25 / 25
595 ms 36264 KB
#include<iostream>
#include<vector>
#include<algorithm>
#define ep emplace
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef pair<int,int>pi;
typedef pair<ll,ll>pl;
const int inf=1e9+7;
const ll INF=1e18;
int tp[2010][2010];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin>>n;
    for(int i=1;i++<n;)
    {
        string s;
        cin>>s;
        for(int j=1;j<i;j++)
            tp[i][j]=tp[j][i]=s[j-1]=='B'?0:1;
    }
    for(int i=0;i++<n;)
    {
        vector<pi>v0,v1;
        vector<int>vv;
        for(int j=0;j++<n;)
            if(i!=j)
                vv.eb(j);
        if(tp[i][vv.back()]==0)
            v0.eb(i,vv.back());
        else
            v1.eb(i,vv.back());
        vv.pop_back();
        for(int&t:vv)
        {
            int mid=(v0.empty()?v1:v0).back().se;
            if(mid==i)
            {
                if(v0.empty())
                {
                    for(pi&t:v1)
                        swap(t.fi,t.se);
                    reverse(all(v1));
                }
                else
                {
                    for(pi&t:v0)
                        swap(t.fi,t.se);
                    reverse(all(v0));
                }
                mid=(v0.empty()?v1:v0).back().se;
            }
            int mcol=tp[mid][t];
            if(v0.empty())
            {
                if(mcol==0)
                    v0.eb(t,mid);
                else
                    v1.eb(mid,t);
            }
            else if(v1.empty())
            {
                if(mcol==0)
                    v0.eb(mid,t);
                else
                    v1.eb(t,mid);
            }
            else
            {
                if(mcol==0)
                {
                    v0.eb(mid,t);
                    int ncol=tp[t][v1.back().fi];
                    if(ncol==1)
                        v1.back().se=t;
                    else
                        v0.eb(t,v1.back().fi),v1.pop_back();
                }
                else
                {
                    v1.eb(mid,t);
                    int ncol=tp[t][v0.back().fi];
                    if(ncol==0)
                        v0.back().se=t;
                    else
                        v1.eb(t,v0.back().fi),v0.pop_back();
                }
            }
        }
        vector<int>vv2;
        reverse(all(v1));
        for(pi&t:v0)
            vv2.eb(t.fi),vv2.eb(t.se);
        for(pi&t:v1)
            vv2.eb(t.se),vv2.eb(t.fi);
        vv2.erase(unique(all(vv2)),vv2.end());
        if(vv2[0]!=i)
            reverse(all(vv2));
        cout<<n<<endl;
        for(int&t:vv2)
            cout<<t<<' ';
        cout<<endl;
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 1 ms 512 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 500 ms 35832 KB Output is correct
17 Correct 478 ms 35960 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 482 ms 35832 KB Output is correct
20 Correct 545 ms 36216 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 477 ms 35960 KB Output is correct
23 Correct 521 ms 35736 KB Output is correct
24 Correct 2 ms 768 KB Output is correct
25 Correct 475 ms 35748 KB Output is correct
26 Correct 485 ms 35832 KB Output is correct
27 Correct 38 ms 4472 KB Output is correct
28 Correct 498 ms 35960 KB Output is correct
29 Correct 503 ms 35832 KB Output is correct
30 Correct 501 ms 35960 KB Output is correct
31 Correct 486 ms 35960 KB Output is correct
32 Correct 487 ms 35704 KB Output is correct
33 Correct 501 ms 35960 KB Output is correct
34 Correct 489 ms 35832 KB Output is correct
35 Correct 487 ms 36088 KB Output is correct
36 Correct 502 ms 36264 KB Output is correct
37 Correct 472 ms 35964 KB Output is correct
38 Correct 480 ms 35960 KB Output is correct
39 Correct 482 ms 35704 KB Output is correct
40 Correct 497 ms 35964 KB Output is correct
41 Correct 529 ms 35832 KB Output is correct
42 Correct 501 ms 35732 KB Output is correct
43 Correct 535 ms 35912 KB Output is correct
44 Correct 506 ms 35704 KB Output is correct
45 Correct 493 ms 35960 KB Output is correct
46 Correct 510 ms 35744 KB Output is correct
47 Correct 512 ms 35960 KB Output is correct
48 Correct 497 ms 35836 KB Output is correct
49 Correct 482 ms 35960 KB Output is correct
50 Correct 499 ms 35812 KB Output is correct
51 Correct 508 ms 35688 KB Output is correct
52 Correct 563 ms 35960 KB Output is correct
53 Correct 507 ms 35840 KB Output is correct
54 Correct 489 ms 35960 KB Output is correct
55 Correct 576 ms 35964 KB Output is correct
56 Correct 568 ms 35960 KB Output is correct
57 Correct 551 ms 35960 KB Output is correct
58 Correct 547 ms 36088 KB Output is correct
59 Correct 544 ms 35960 KB Output is correct
60 Correct 539 ms 36096 KB Output is correct
61 Correct 586 ms 35972 KB Output is correct
62 Correct 541 ms 36080 KB Output is correct
63 Correct 555 ms 35972 KB Output is correct
64 Correct 539 ms 36088 KB Output is correct
65 Correct 545 ms 36088 KB Output is correct
66 Correct 532 ms 35960 KB Output is correct
67 Correct 531 ms 35920 KB Output is correct
68 Correct 544 ms 35960 KB Output is correct
69 Correct 543 ms 36088 KB Output is correct
70 Correct 560 ms 36120 KB Output is correct
71 Correct 541 ms 36244 KB Output is correct
72 Correct 532 ms 36088 KB Output is correct
73 Correct 535 ms 36004 KB Output is correct
74 Correct 535 ms 36088 KB Output is correct
75 Correct 540 ms 36104 KB Output is correct
76 Correct 543 ms 35936 KB Output is correct
77 Correct 550 ms 36000 KB Output is correct
78 Correct 552 ms 35968 KB Output is correct
79 Correct 544 ms 35896 KB Output is correct
80 Correct 535 ms 35960 KB Output is correct
81 Correct 536 ms 36088 KB Output is correct
82 Correct 535 ms 36216 KB Output is correct
83 Correct 534 ms 36084 KB Output is correct
84 Correct 550 ms 36076 KB Output is correct
85 Correct 543 ms 36088 KB Output is correct
86 Correct 547 ms 36024 KB Output is correct
87 Correct 35 ms 4472 KB Output is correct
88 Correct 33 ms 4472 KB Output is correct
89 Correct 548 ms 36088 KB Output is correct
90 Correct 561 ms 36240 KB Output is correct
91 Correct 35 ms 4600 KB Output is correct
92 Correct 43 ms 4448 KB Output is correct
93 Correct 595 ms 36088 KB Output is correct
94 Correct 538 ms 35960 KB Output is correct
95 Correct 32 ms 4472 KB Output is correct
96 Correct 33 ms 4472 KB Output is correct
97 Correct 532 ms 36216 KB Output is correct
98 Correct 526 ms 36144 KB Output is correct