QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#108440#6396. Puzzle: Kusabiberarchegas#WA 92ms8956kbC++175.7kb2023-05-25 01:25:312023-05-25 01:25:34

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-25 01:25:34]
  • 评测
  • 测评结果:WA
  • 用时:92ms
  • 内存:8956kb
  • [2023-05-25 01:25:31]
  • 提交

answer

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <random>
// #include <bits/stdc++.h>

using namespace std;
#define F first
#define S second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef pair<ll,ll> pll;
typedef vector<string> vs;
typedef vector<pll> vpl;
typedef vector<int> vi;

ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down

#define Unique(v) sort(all(v));v.erase(unique(all(v)),v.end());

const int maxn = 100000 + 10;

// const int inf = 1000000000;
// const ll inf = 1000000000000000000LL;

// const ll mod = 1000000007; // 10^9 + 7
// const ll mod = (119 << 23) + 1; // 998244353

int n;

int type[maxn];
int remain[maxn];

int depth[maxn];

vector<int> adj[maxn];

vector<pii> answer;

void impossible()
{
    cout << "NO" << endl;
    exit(0); 
}

int getID(string s)
{
    if( s == "-" )
        return -1;

    if( s == "Tong" )
        return 0;

    if( s == "Chang" )
        return 1;

    return 2;
}

vector<int> removeIndex(vector<int>& v, int id)
{
    vector<int> ans;

    for(int i = 0 ; i < (int)v.size() ; i++)
        if( i != id ) ans.push_back( v[i] );

    return ans;
}

bool check(vector<int> a, vector<int> b)
{
    for(int i = 0 ; i < (int)a.size() ; i++)
        if( depth[ a[i] ] <= depth[ b[i] ] ) return false;

    return true;
}

void pairing(vector<int> a, vector<int> b)
{
    for(int i = 0 ; i < (int)a.size() ; i++)
        answer.push_back( { a[i] , b[i] } );
}

void dfs(int node, int anc, int dp)
{
    depth[node] = dp;
    remain[node] = -1;

    for(int neighbor: adj[node])
        dfs( neighbor , node , dp + 1 );

    vector<int> types[3];

    if( type[node] != -1 )
        types[ type[node] ].push_back( node );

    for(int neighbor: adj[node])
    {
        if( remain[neighbor] == -1 )
            continue;

        int r = remain[neighbor];
        types[ type[r] ].push_back( r );
    }

    // cout << "-------- node = " << node << endl;

    // cout << "0 -> ";

    // for(int x: types[0])
    //     cout << x << " ";

    // cout << endl << "1 -> ";

    // for(int x: types[1])
    //     cout << x << " ";

    // cout << endl << "2 -> ";

    // for(int x: types[2])
    //     cout << x << " ";

    // cout << endl << endl;

    map<int,int> idNode;

    for(int x: types[0])
    {
        if( idNode.find( depth[x] ) != idNode.end() )
        {
            answer.push_back( { x , idNode[ depth[x] ] } );
            idNode.erase( depth[x] );
        }
        else
            idNode[ depth[x] ] = x;
    }

    if( (int)idNode.size() >= 2 )
        impossible();

    sort( types[1].begin() , types[1].end() , [&](int i, int j){
        return depth[i] < depth[j];
    });

    sort( types[2].begin() , types[2].end() , [&](int i, int j){
        return depth[i] < depth[j];
    });

    int d = (int)types[1].size() - (int)types[2].size();

    if( abs(d) >= 2 )
        impossible();

    if( abs(d) == 1 && (int)idNode.size() == 1 )
        impossible();

    if( d == 0 )
    {
        if( !check( types[1] , types[2] ) )
            impossible();

        pairing( types[1] , types[2] );
    }

    if( (int)idNode.size() == 1 )
    {
        auto it = idNode.begin();
        remain[node] = it->second;

        // cout << "entrei" << endl;

        return;
    }

    if( d == 0 )
        return;

    if( d == 1 )
    {
        int l = 0, r = (int)types[1].size() - 1;

        if( !check( removeIndex( types[1] , l ) , types[2] ) )
            impossible();

        while( l < r )
        {
            int m = ( l + r )/2;
            if( l == r - 1 ) m = r;

            if( check( removeIndex( types[1] , m ) , types[2] ) )
                l = m;
            else
                r = m - 1;
        }

        remain[node] = types[1][l];
        types[1] = removeIndex( types[1] , l );
    }

    if( d == -1 )
    {
        // cout << "aqui" << endl;
        int l = 0, r = (int)types[2].size() - 1;

        if( !check( types[1] , removeIndex( types[2] , r ) ) ) 
            impossible();

        while( l < r )
        {
            int m = ( l + r )/2;    
            
            if( check( removeIndex( types[1] , m ) , types[2] ) )
                r = m;
            else
                l = m + 1;
        }

        remain[node] = types[2][r];
        types[2] = removeIndex( types[2] , r );
    }

    // cout << "remain " << remain[node] << endl;
    pairing( types[1] , types[2] );
}

void solve(int testcase)
{
    cin >> n;

    type[1] = -1;

    for(int i = 1 ; i < n ; i++)
    {
        int u, v;
        string aux;
        cin >> u >> v >> aux;

        adj[v].push_back( u );
        type[u] = getID( aux );
    }

    dfs( 1 , 1 , 0 );

    if( remain[1] != -1 )
        impossible();

    cout << "YES" << endl;

    for(auto [x, y]: answer)
        cout << x << " " << y << endl;
}

int main()
{
    ios_base::sync_with_stdio(false);  
    cin.tie(NULL);

    int t = 1;
    // cin >> t;

    for(int testcase = 1 ; testcase <= t ; testcase++)
        solve( testcase );
}

詳細信息

Test #1:

score: 100
Accepted
time: 4ms
memory: 5832kb

input:

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

output:

YES
5 4
8 6

result:

ok Correct.

Test #2:

score: 0
Accepted
time: 3ms
memory: 5760kb

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
10 3
9 8
6 2
5 7

result:

ok Correct.

Test #3:

score: 0
Accepted
time: 3ms
memory: 5884kb

input:

2
2 1 Tong

output:

NO

result:

ok Correct.

Test #4:

score: -100
Wrong Answer
time: 92ms
memory: 8956kb

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:

YES
56115 46666
88119 38802
93143 10006
31531 76473
42604 15988
30148 6569
11412 2008
64525 46703
71289 70618
81204 47748
42353 20514
97634 46131
83516 82155
66867 62410
15712 9975
4978 3205
83026 5622
48783 10902
82167 30893
93809 65878
66951 33377
94549 66936
79128 64411
22475 8453
54702 36857
905...

result:

wrong answer Pair 79857-40731 symbol not satisfied.