답안 #802675

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
802675 2023-08-02T13:16:58 Z ashcomp LOSTIKS (INOI20_lostiks) C++14
0 / 100
357 ms 184172 KB
#include <bits/stdc++.h>
 
using namespace std;
 
#define all(x) x.begin() , x.end()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define F first
#define S second
#define wall cerr<<"------------------------"<<endl
 
typedef long long ll;
 
const ll INF = 1e18;
const ll maxn = 1e6 + 10;
const ll mod = 1e9 + 7;
 
int n , m , k , ST , EN;
int h[maxn] , p[maxn] , st[maxn] , en[maxn];
int a[maxn] , rmq[maxn][21] , ps_rmq[maxn][21] , rrmq[maxn][21] , ps_rrmq[maxn][21] , len[maxn];
vector<int> g[maxn] , LSA;
bool mrk[maxn];
 
void DFS(int v)
{
        mrk[v] = 1;
        LSA.push_back(v);
        st[v] = LSA.size()-1;
        for(auto u : g[v]){
 
                if(mrk[u]==1)continue;
                p[u] = v;
                h[u] = h[v]+1;
                DFS(u);
                LSA.push_back(v);
        }
        en[v] = LSA.size()-1;
}
void pre_LCA()
{
        LSA.push_back(0);
        p[ST] = -1;
        h[ST] = 1;
        DFS(ST);
        m = LSA.size()-1;
        for(int i=1; i<=m; i++){
                a[i] = h[LSA[i]];
        }
        for(int j=0; j<21; j++){
                for(int i=1; i<=m; i++){
                        if(j==0){
                                rmq[i][j] = a[i];
                                rrmq[i][j] = a[i];
                                ps_rmq[i][j] = i;
                                ps_rrmq[i][j] = i;
                                continue;
                        }
                        if((i+(1<<j)-1)<=m){
                                rmq[i][j] = rmq[i][j-1];
                                ps_rmq[i][j] = ps_rmq[i][j-1];
                                if(rmq[i][j-1] > rmq[i+(1<<(j-1))][j-1]){
                                        rmq[i][j] = rmq[i+(1<<(j-1))][j-1];
                                        ps_rmq[i][j] = ps_rmq[i+(1<<(j-1))][j-1];
                                }
                        }
                        if((i-(1<<j)+1)>0){
                                rrmq[i][j] = rrmq[i][j-1];
                                ps_rrmq[i][j] = ps_rrmq[i][j-1];
                                if(rrmq[i][j-1] > rrmq[i-(1<<(j-1))][j-1]){
                                        rrmq[i][j] = rrmq[i-(1<<(j-1))][j-1];
                                        ps_rrmq[i][j] = ps_rrmq[i-(1<<(j-1))][j-1];
                                }
                        }
                }
        }
        int PS = 0;
        for(int i=1; i<=m; i++){
                if(((1<<PS)<<1)<=i){
                        PS++;
                }
                len[i] = PS;
        }
}//OK
int LCA(int v , int u)
{
        int l = min(st[v],st[u]) , r=max(st[v],st[u]);
        int ln = len[r-l+1];
        int x = rmq[l][ln] , y = rrmq[r][ln];
        if(x < y){
                int pos = ps_rmq[l][ln];
                return LSA[pos];
        }else{
                int pos = ps_rrmq[r][ln];
                return LSA[pos];
        }
}
 
int times=0;
map<int,bool> mp[maxn];
vector<int> ORDER;
vector<pii> eg[maxn];
int dfs(int v , int tm , int end)
{
        //cout<<v<<"-->"<<end<<" : "<<tm<<"\n";
        if(times>20){
                return -1;
        }
        if(v==end){
                return 1;
        }
        for(auto e : eg[v]){
                int u=e.F , w=e.S;
                int now = h[end]+h[v]-h[LCA(end,v)]-h[LCA(end,v)];
                int then = h[end]+h[u]-h[LCA(end,u)]-h[LCA(end,u)];
                if(then < now){
                        if(w==0){
                                int tp = dfs(u,tm,end);
                                if(tp==-1)return -1;
                        }else{
                                if(mp[v][u]==0){
                                        times++;
                                        int tp = dfs(v,times,w);
                                        if(tp==-1){
                                                return -1;
                                        }
                                        ORDER.push_back(w);
                                        ORDER.push_back(v);
                                        mp[v][u] = 1;
                                        mp[u][v] = 1;
                                        tp = dfs(u,tm,end);
                                        if(tp==-1)return -1;
                                }else{
                                        int tp = dfs(u,tm,end);
                                        if(tp==-1)return -1;
                                }
                        }
                }
        }
        return 1;
}

void D(int v)
{
        mrk[v] = 0;
        for(auto u : g[v]){
                if(mrk[u]==0)continue;
                h[u] = h[v]+1;
                D(u);
        }
}

int main()
{
        ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
        cin>>n>>ST>>EN;
        for(int i=1; i<n; i++){
                int v,u,w; cin>>v>>u>>w;
                if(w==0){
                        mp[v][u] = 1;
                        mp[u][v] = 1;
                }
                eg[v].push_back({u,w});
                eg[u].push_back({v,w});
                g[v].push_back(u);
                g[u].push_back(v);
        }
        pre_LCA();
        ORDER.push_back(ST);
        int tp = dfs(ST,1,EN);
        ORDER.push_back(EN);
        if(tp==-1){
                cout<<"-1"<<endl;
        }else{
                ll ans = 0 , sz=ORDER.size();
                for(int i=1; i<sz; i++){
                        int v = ORDER[i-1] , u=ORDER[i];
                        //ans += h[v]+h[u]-h[LCA(v,u)]-h[LCA(v,u)]; 
                        h[v] = 0;
                        for(int i=1; i<=n; i++)mrk[i]=1;
                        D(v);
                        ans += h[u];
                        //cout<<v<<"=>"<<u<<" : "<<h[u]<<"\n";
                }
                cout<<ans;
        }
        return 0;
}
/*
9
2 9
1 2 0
1 4 0
1 7 0
3 4 7
3 5 4
5 6 0
7 8 6
8 9 0
ans = 14;
*/
/**
4
1 4
1 2 0
1 3 2
3 4 1
ans = 4;
*/
/*
5
3 1
1 2 5
2 3 4
3 4 0
4 5 2
ans = 10;
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 94412 KB Output is correct
2 Correct 44 ms 94344 KB Output is correct
3 Correct 294 ms 181112 KB Output is correct
4 Correct 263 ms 181172 KB Output is correct
5 Correct 252 ms 181172 KB Output is correct
6 Correct 319 ms 181184 KB Output is correct
7 Correct 303 ms 181296 KB Output is correct
8 Correct 349 ms 181452 KB Output is correct
9 Correct 357 ms 181724 KB Output is correct
10 Correct 317 ms 181388 KB Output is correct
11 Correct 334 ms 181364 KB Output is correct
12 Correct 278 ms 182404 KB Output is correct
13 Incorrect 242 ms 184172 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 94232 KB Output is correct
2 Correct 54 ms 94260 KB Output is correct
3 Incorrect 48 ms 94416 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 94412 KB Output is correct
2 Correct 44 ms 94344 KB Output is correct
3 Correct 294 ms 181112 KB Output is correct
4 Correct 263 ms 181172 KB Output is correct
5 Correct 252 ms 181172 KB Output is correct
6 Correct 319 ms 181184 KB Output is correct
7 Correct 303 ms 181296 KB Output is correct
8 Correct 349 ms 181452 KB Output is correct
9 Correct 357 ms 181724 KB Output is correct
10 Correct 317 ms 181388 KB Output is correct
11 Correct 334 ms 181364 KB Output is correct
12 Correct 278 ms 182404 KB Output is correct
13 Incorrect 242 ms 184172 KB Output isn't correct
14 Halted 0 ms 0 KB -