QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#386855#6396. Puzzle: KusabiCRN2010WA 19ms10380kbC++205.2kb2024-04-11 20:43:302024-04-11 20:43:30

Judging History

你现在查看的是最新测评结果

  • [2024-04-11 20:43:30]
  • 评测
  • 测评结果:WA
  • 用时:19ms
  • 内存:10380kb
  • [2024-04-11 20:43:30]
  • 提交

answer

#pragma GCC optimize(3) 
#pragma GCC optimize("Ofast,no-stack-protector")
#include <bits/stdc++.h>

const int N = 1e5 + 10 ; 

int n ; 
int idx[N] , d[N] , color[N] ; 
std::vector<int> edges[N] ;
std::vector<std::pair<int , int>> ret ; 

void dfs(int v , int f , int dist) {
    d[v] = dist ; 

    std::vector<int> c1 , c2 , c3 ; 
    if ( color[v] == 1 ) c1.push_back(v) ;
    else if ( color[v] == 2 ) c2.push_back(v) ;
    else if ( color[v] == 3 ) c3.push_back(v) ;
    color[v] = 0 ;

    for(auto u : edges[v]) {
        if ( u != f ) 
        {
            dfs(u , v , dist + 1) ;
            int k = idx[u] ;

            if ( color[k] == 1 ) c1.push_back(k) ;
            else if ( color[k] == 2 ) c2.push_back(k) ;
            else if ( color[k] == 3 ) c3.push_back(k) ;
        }
    }
    sort(c1.begin() , c1.end() , [&](const int a,const int b){return d[a] > d[b];}) ;
    sort(c2.begin() , c2.end() , [&](const int a,const int b){return d[a] < d[b];}) ;
    


    int x = c1.size() , y = c2.size() , z = c3.size() , ok = 0 ;


    for(int i = 0 ; i < c3.size() ; ++ i)
    {
        if ( c3[i] == -1 ) continue ;
        for(int j = i + 1 ; j < c3.size() ; ++ j)
            if ( d[c3[i]] == d[c3[j]] ) {
                ret.push_back({c3[i] , c3[j]}) ;
                c3[i] = c3[j] = -1 ;
                z -= 2 ;
                break ;
            }
    }   

    if ( z > 1 ) {
        std::cout << "NO\n" ;
        exit(0) ;
    }
    else if ( z == 1 ) {
        for(auto p : c3) if ( d[p] != -1 ) {
            idx[v] = p ;
            color[idx[v]] = 3  ;
            
            ok = 1 ;
            break ;
        }
    }

    // if ( v == 2 ) {
    //     std::cout << '\n' ;
    //     std::cout << x << ' ' << y << '\n' ;
    //     for(auto p : c1) {
    //         std::cout << p << ' ' << d[p] << ' ' ;
    //     }
    //     std::cout << '\n' ;
    //     for(auto p : c2) {
    //         std::cout << p << ' ' << d[p] << ' ' ;
    //     }
    //     std::cout << '\n' ;
    //     std::cout << '\n' ;
    // }

    for(int i = 0 ; i < c1.size() ; ++ i)
    {
        int id = -1 ; 
        if ( c1[i] == -1 ) continue ;
        for(int j = 0 ; j < c2.size() ; ++ j)
        {
            if ( c2[j] == -1 ) continue ;
            if ( d[c1[i]] < d[c2[j]] ) {
                if ( id == -1  ) id = j ;
                else if ( d[c2[id]] > d[c2[j]] ) id = j ;
            }
        }
        if ( id != -1 ) {
            ret.push_back({c1[i] , c2[id]}) ;
            c1[i] = c2[id] = -1 ; 
            --x , --y ;
        }
    }
    // if ( v == 2 ) {
    //     std::cout << x << ' ' << y << '\n' ; 
    //     std::cout << '\n' ;
    //     for(auto p : c1) {
    //         std::cout << p << ' ' << d[p] << ' ' ;
    //     }
    //     std::cout << '\n' ;
    //     for(auto p : c2) {
    //         std::cout << p << ' ' << d[p] << ' ' ;
    //     }
    //     std::cout << '\n' ;
    //     std::cout << '\n' ;
    // }
    //     std::cout << v << '\n' ;
    //     for(int i = 1 ; i <= n ; ++ i)
    //     {
    //         std::cout << i << " : " << color[i] << ' ' << d[i] << ' ' << idx[i] << '\n' ;
    //     }

    if ( x + y > 1 ) {
        // std::cout << v << '\n' ;
        // for(auto p : c1) {
        //     if ( p != -1 ) std::cout << p << '\n' ;
        // }
        // for(auto p : c2) {
        //     if ( p != -1 ) std::cout << p << '\n' ;
        // }

    
        //std::cout << x << ' ' << y << '\n' ;
        std::cout << "2\n" ;
        exit(0) ;
    }
    else if ( x + y == 1 && ok ) {
        std::cout << "1\n" ;
        exit(0) ;
    }
    else if ( x ) 
    {
        for(auto p : c1) if ( p != -1 ) {
            idx[v] = p ;
            color[idx[v]] = 1 ;
            break ;
        }
    }
    else if ( y )
    {
        for(auto p : c2) if ( p != -1 ) {
            idx[v] = p ;
            color[idx[v]] = 2 ;
            break ;
        }
    }
        // std::cout << v << '\n' ;
        // for(int i = 1 ; i <= n ; ++ i)
        // {
        //     std::cout << i << " : " << color[i] << ' ' << d[i] << ' ' << idx[i] << '\n' ;
        // }
}



int main()
{
    std::cin.tie(nullptr)->std::ios::sync_with_stdio(false) ;

#ifdef LOCAL
    freopen("D:\\Codes\\in.txt" , "r" , stdin) ;
    freopen("D:\\Codes\\out.txt" , "w" , stdout) ;
#endif

    std::cin >> n ; 
    idx[n] = n ; 
    for(int i = 1 ; i < n ; ++ i) {
        idx[i] = i ; 
        int u , v ;
        std::string col ; 
        std::cin >> u >> v >> col ; 
        if ( col == "Duan" ) color[u] = 1 ;
        else if ( col == "Chang" ) color[u] = 2 ;
        else if ( col == "Tong" ) color[u] = 3 ; 
        edges[u].push_back(v) ;
        edges[v].push_back(u) ;
    }

    dfs(1 , 0 , 0) ;
    if ( n == 2 ) return std::cout << "NO\n" , 0 ;

    

    std::cout << "YES\n" ;
    for(auto p : ret) {
        std::cout << p.first << ' ' << p.second << '\n' ;
    }

    // for(int i = 1 ; i <= n ; ++ i)
    // {
    //     std::cout << i << " : " << color[i] << ' ' << d[i] << ' ' << idx[i] << '\n' ;
    // }

    return 0 ;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3924kb

input:

8
2 1 -
3 1 -
4 2 Tong
5 2 Tong
6 3 Duan
7 3 -
8 7 Chang

output:

YES
4 5
6 8

result:

ok Correct.

Test #2:

score: 0
Accepted
time: 1ms
memory: 3624kb

input:

10
2 1 Duan
3 2 Duan
4 2 -
5 4 Chang
6 2 Chang
7 1 Duan
8 6 Tong
9 6 Tong
10 3 Chang

output:

YES
3 10
8 9
2 6
7 5

result:

ok Correct.

Test #3:

score: 0
Accepted
time: 1ms
memory: 5888kb

input:

2
2 1 Tong

output:

NO

result:

ok Correct.

Test #4:

score: -100
Wrong Answer
time: 19ms
memory: 10380kb

input:

100000
2 1 Duan
3 1 Duan
4 3 -
5 4 Duan
6 3 -
7 4 Duan
8 4 -
9 8 -
10 7 Duan
11 9 -
12 7 Duan
13 7 Duan
14 8 Duan
15 13 -
16 10 Duan
17 11 Duan
18 12 -
19 1 Duan
20 5 Duan
21 4 Duan
22 14 Duan
23 16 -
24 22 Duan
25 16 Duan
26 13 -
27 13 -
28 17 -
29 5 Duan
30 22 -
31 23 -
32 9 Duan
33 5 -
34 30 Duan...

output:

NO

result:

wrong answer Jury has answer but participant has not.