QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#791132#6742. LeavesHJR#WA 1ms3832kbC++174.1kb2024-11-28 17:03:272024-11-28 17:03:29

Judging History

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

  • [2024-11-28 17:03:29]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3832kb
  • [2024-11-28 17:03:27]
  • 提交

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);
    };
    array<int,2> temp = {0,0};
    for(int i = 1; e[i].size() ;i = e[i][1])
    {
        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();j++)
        {
            int lca = LCA(ord[0],ord[j]);
            //cout<<ord[0] <<" "<<ord[j]<<" "<<lca<<endl;
            if(m >= lca)
            {
               // cout<<ord[0]<<" "<<ord[j]<<endl;
                temp = {ord[0],ord[j]};
                update(ord[0],ord[j]);
                m -= lca;
                break;
            }
        }
    }

    ord.clear();
    dfs(dfs,1);
    if(m % 2)
    {
        if(temp[0] != 0)
        {
            auto [x,y] = temp;
            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);
                        m--;
                        break;
                    }
                    y = fa[y];
                }
                else{
                    if(son_vis[x] != son_vis[y]){
                        SWAP(y);
                        m--;
                        break;
                    }
                    x = fa[x];
                    y = fa[y];
                }
            }
            if(m % 2)
                SWAP(y);
            ord.clear();
            dfs(dfs,1);
        }
    }


    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: 1ms
memory: 3608kb

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: 3616kb

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: 3672kb

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: 3664kb

input:

1 0
2 1000000000

output:

1000000000 

result:

ok 1 number(s): "1000000000"

Test #5:

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

input:

3 1
1 2 3
2 1
2 2

output:

2 1 

result:

ok 2 number(s): "2 1"

Test #6:

score: -100
Wrong Answer
time: 0ms
memory: 3828kb

input:

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

output:

2 1 4 3 

result:

wrong answer 1st numbers differ - expected: '1', found: '2'