QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#791410#6742. LeavesHJR#WA 2ms3820kbC++174.3kb2024-11-28 18:28:402024-11-28 18:28:40

Judging History

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

  • [2024-11-28 18:28:40]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:3820kb
  • [2024-11-28 18:28:40]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define debug(x) cout<<#x<<": "<<x<<endl
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
using ll=long long;
using ull=unsigned long long;
void solve(){
    int n,m;
    cin >> n >> m;
    vector<vector<int>>e(n+1);
    vector<int>fa(n+1);
    vector<int>son_vis(n+1);
    vector<int>label(n+1);
    vector<int>dep(n+1);
    vector<array<int,2>>cmp;

    for(int i = 1;i <= n;i++)
    {
        int opt;
        cin>>opt;
        if(opt == 1)
        {
            int a,b;
            cin>>a>>b;
            e[i].push_back(a);
            e[i].push_back(b);
            son_vis[a] = 0,son_vis[b] = 1;
            fa[a] = fa[b] = i;
            dep[a] = dep[b] = dep[i] + 1;
        }
        else
        {
            cin >> label[i];
            cmp.push_back({label[i],i});        
        }
    }

    sort(cmp.begin(),cmp.end());

    vector<int>ord;
    auto dfs = [&](auto &&self,int u) ->void
    {
        for(auto v:e[u])
            self(self,v);
        if(e[u].empty())
            ord.push_back(u);
    };
    auto LCA = [&](int x,int y)
    {
        int len = 1;
        while(fa[x] != fa[y])
        {
            if(dep[x] > dep[y])
                x = fa[x];
            else if(dep[x] < dep[y]){
                len += (son_vis[x] != son_vis[y]);
                y = fa[y];
            }
            else{
                len += (son_vis[x] != son_vis[y]);
                x = fa[x];
                y = fa[y];
            }
        }   
        return len;
    };
    auto SWAP = [&](int y)
    {
        int f = fa[y];
        swap(e[f][0],e[f][1]);
        swap(son_vis[e[f][0]],son_vis[e[f][1]]);
    };
    auto update = [&](int x,int y)
    {
        while(fa[x] != fa[y])
        {
            if(dep[x] > dep[y])
                x = fa[x];
            else if(dep[x] < dep[y]){
                if(son_vis[y] != son_vis[x])
                    SWAP(y);
                y = fa[y];
            }
            else{
                if(son_vis[x] != son_vis[y])
                    SWAP(y);
                x = fa[x];
                y = fa[y];
            }
        }
        SWAP(y);
    };

    auto check = [&](int i)
    {
        ord.clear();
        dfs(dfs,i);

        sort(ord.begin()+1,ord.end(),[&](const int &x,const int &y){
            return label[x] < label[y];
        });
        for(int j = 1;j<ord.size() && label[ord[j]] < label[ord[0]];j++)
        {
            int lca = LCA(ord[0],ord[j]);
            //cout<<ord[0] <<" "<<ord[j]<<" "<<lca<<endl;
            if(m >= lca)
            {
               // cout<<ord[0]<<" "<<ord[j]<<endl;
                update(ord[0],ord[j]);
                m -= lca;
                break;
            }
        }
    };
    for(int i = 1; e[i].size() ;i = e[i][1])
    {
        check(i);
        for(int j = ord[0];j != i;j = fa[j])
        {
            if(e[j].size())
            {
                check(e[j][1]);
            }
        }
    }

    ord.clear();
    dfs(dfs,1);
    auto chmin = [&](vector<int> &x,vector<int> &y)
    {
        for(int i = 0;i < x.size();i++)
            if(x[i] != label[y[i]])
            {
                if(x[i] > label[y[i]])
                {
                    for(int j = 0;j < x.size();j++)
                    {
                        x[j] = label[y[j]];
                    }
                }
                return;
            }
    };
    if(m % 2)
    {
        vector<int>ans(ord.size(),1e9);
        for(int i = 1;i <= n;i++)
        {
            if(e[i].size())
            {
                ord.clear();
                SWAP(e[i][1]);
                dfs(dfs,1);
                chmin(ans,ord);
                SWAP(e[i][1]);
            }
        }
        for(auto i:ans)
            cout<<i<<" ";
        cout<<endl;
        return;
    }


    for(auto i:ord)
        cout<<label[i]<<" ";
    cout<<endl;
    
}
signed main(){
    ios::sync_with_stdio(0);
    cout.tie(0);
    cin.tie(0);
    int t = 1;
    //cin>>t;
    while(t--){
        solve();
    }
}
/*
贡献法
正难则反
数小状压
关系连边
拆位
广义单调性
最长转最短
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3616kb

input:

3 0
1 2 3
2 1
2 2

output:

1 2 

result:

ok 2 number(s): "1 2"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3620kb

input:

7 1
1 2 3
1 4 5
1 6 7
2 4
2 2
2 3
2 1

output:

2 4 3 1 

result:

ok 4 number(s): "2 4 3 1"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3668kb

input:

7 2
1 2 3
1 4 5
1 6 7
2 4
2 2
2 3
2 1

output:

1 3 4 2 

result:

ok 4 number(s): "1 3 4 2"

Test #4:

score: 0
Accepted
time: 0ms
memory: 3668kb

input:

1 0
2 1000000000

output:

1000000000 

result:

ok 1 number(s): "1000000000"

Test #5:

score: 0
Accepted
time: 0ms
memory: 3784kb

input:

3 1
1 2 3
2 1
2 2

output:

2 1 

result:

ok 2 number(s): "2 1"

Test #6:

score: 0
Accepted
time: 0ms
memory: 3820kb

input:

7 2
1 2 3
1 4 5
1 6 7
2 1
2 2
2 3
2 4

output:

1 2 3 4 

result:

ok 4 number(s): "1 2 3 4"

Test #7:

score: -100
Wrong Answer
time: 2ms
memory: 3684kb

input:

999 480
1 3 2
1 4 5
1 6 7
1 9 8
1 10 11
1 13 12
1 14 15
1 16 17
1 19 18
1 21 20
1 23 22
1 25 24
1 27 26
1 28 29
1 30 31
1 33 32
1 35 34
1 37 36
1 38 39
1 41 40
1 42 43
1 45 44
1 46 47
1 48 49
1 51 50
1 52 53
1 55 54
1 56 57
1 58 59
1 61 60
1 62 63
1 64 65
1 67 66
1 69 68
1 71 70
1 73 72
1 74 75
1 76...

output:

34826804 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 248820103 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 7...

result:

wrong answer 9th numbers differ - expected: '248820103', found: '763875883'