CSES - HIIT Open 2019 - Results
Submission details
Task:HIIT Speedrun
Sender:Game of Nolife
Submission time:2019-05-25 11:52:55 +0300
Language:C++
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.03 sdetails
#2ACCEPTED0.01 sdetails
#3ACCEPTED0.03 sdetails
#4ACCEPTED0.02 sdetails
#5ACCEPTED0.05 sdetails
#6ACCEPTED0.02 sdetails
#7ACCEPTED0.01 sdetails
#8ACCEPTED0.02 sdetails
#9ACCEPTED0.02 sdetails
#10ACCEPTED0.02 sdetails
#11ACCEPTED0.01 sdetails
#12ACCEPTED0.02 sdetails
#13ACCEPTED0.02 sdetails

Code

#include <bits/stdc++.h>
#define F first
#define S second
#define X real()
#define Y imag()
using namespace std;
typedef long long ll;
typedef long double ld;

vector<int> g[111];

int d[111][(1<<3)][2];

int inf=1e9;

int addc(int b, char c, int x, int mid){
    if (x==mid) return b;
    if (c=='H'){
        b|=1;
    } else if(c=='I'){
        b|=2;
    } else if(c=='T'){
        b|=4;
    }
    return b;
}

int n;
string s;

int test(int mid){    
    for (int i=1;i<=n;i++){
        for (int b=0;b<(1<<3);b++){
            for (int y=0;y<2;y++){
                d[i][b][y]=inf;
            }
        }
    }
    d[1][addc(0, s[0], 1, mid)][1==mid]=0;
    queue<tuple<int, int, int>> bfs;
    bfs.push(make_tuple(1, addc(0, s[0], 1, mid), 1==mid));
    while (!bfs.empty()){
        int x,b,y;
        tie(x,b,y) = bfs.front();
        bfs.pop();
        int td=d[x][b][y];
        for (int nx:g[x]){
            //if (y && nx==mid) continue;
            int nb=addc(b, s[nx-1], nx, mid);
            int ny=(y || nx==mid);
            if (d[nx][nb][ny]>td+1){
                d[nx][nb][ny]=td+1;
                bfs.push(make_tuple(nx, nb, ny));
            }
        }
    }
    return d[n][7][1];
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int m;
    cin>>n>>m;
    cin>>s;
    for (int i=0;i<m;i++){
        int a,b;
        cin>>a>>b;
        g[a].push_back(b);
        g[b].push_back(a);
    }

    int ans=inf;
    for (int i=1;i<=n;i++){
        if (s[i-1]=='I'){
            ans=min(ans, test(i));
        }
    }

    if (ans<inf){
        cout<<ans<<endl;
    }else{
        cout<<"IMPOSSIBLE"<<endl;
    }
}

Test details

Test 1

Verdict: ACCEPTED

input
4 0
HIIT

correct output
IMPOSSIBLE

user output
IMPOSSIBLE

Test 2

Verdict: ACCEPTED

input
4 3
HIIT
1 2
2 3
3 4

correct output
3

user output
3

Test 3

Verdict: ACCEPTED

input
4 3
HIIT
4 3
3 2
2 1

correct output
3

user output
3

Test 4

Verdict: ACCEPTED

input
6 5
XHIITX
1 2
1 3
1 4
...

correct output
9

user output
9

Test 5

Verdict: ACCEPTED

input
100 5000
TIIIIIIITHHHTHHHIHIIHIHHHITHIH...

correct output
3

user output
3

Test 6

Verdict: ACCEPTED

input
100 99
HIIIIIIIIIIIIIIIIIIIIIIIIIIIII...

correct output
99

user output
99

Test 7

Verdict: ACCEPTED

input
10 10
AAAHIITAAA
1 2
2 3
3 4
...

correct output
9

user output
9

Test 8

Verdict: ACCEPTED

input
9 9
HIIIIIIIT
1 2
2 3
3 4
...

correct output
4

user output
4

Test 9

Verdict: ACCEPTED

input
9 9
HIIIIAAAT
1 2
2 3
3 4
...

correct output
5

user output
5

Test 10

Verdict: ACCEPTED

input
100 99
HIIIIIIIIIIIIIIIIIIIIIIIIIIIII...

correct output
197

user output
197

Test 11

Verdict: ACCEPTED

input
8 8
HIITHIIT
1 2
2 3
3 4
...

correct output
IMPOSSIBLE

user output
IMPOSSIBLE

Test 12

Verdict: ACCEPTED

input
6 5
HIIITI
1 2
2 3
3 4
...

correct output
IMPOSSIBLE

user output
IMPOSSIBLE

Test 13

Verdict: ACCEPTED

input
100 99
HHHHHHHHHHHHHHHHHHHHHHHHHIIIII...

correct output
99

user output
99