답안 #61704

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
61704 2018-07-26T11:17:17 Z hamzqq9 Min-max tree (BOI18_minmaxtree) C++14
22 / 100
494 ms 32480 KB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 1000000000000000000
#define iii pair<int,pair<int,int> >
#define st first
#define nd second
#define pb push_back
#define umax(x,y) x=max(x,(y))
#define pw(x) (1<<(x))
#define N 70005
#define LOG 21
#define all(x) x.begin(),x.end()
#define sz(x) x.size()

int n,x,y,z,q;
vector<int> v[N],del[N];
multiset<int> add[N];
int xcost[N],ncost[N],baba[LOG+2][N],der[N];
vector<iii> Min,Max;

int lca(int x,int y) {

  if(der[x]<der[y]) swap(x,y);

  int dx=der[x];

  for(int i=LOG-1;i>=0;i--) {

    if(dx-pw(i)>=der[y]) {

      x=baba[i][x];
      dx-=pw(i);

    }

  }

  if(x==y) return x;

  for(int i=LOG-1;i>=0;i--) {

    if(baba[i][x]!=baba[i][y]) {

      x=baba[i][x];
      y=baba[i][y];

    }

  }

  return baba[0][x];

}

void dfs3(int node,int ata) {

  for(int i:v[node]) {

    if(i==ata) continue ;

    dfs3(i,node);

    if(sz(add[i])>sz(add[node])) swap(add[i],add[node]);

    for(auto x:add[i]) add[node].insert(x);

    add[i].clear();

  }

  for(int i:del[node]) {

    add[node].erase(add[node].find(i));
    add[node].erase(add[node].find(i));

  }

  if(node==1) return ;

  if(sz(add[node])) xcost[node]=*add[node].begin(),ncost[node]=*add[node].rbegin();
  else xcost[node]=123,ncost[node]=123;

}

void build_lca() {

  for(int i=1;i<LOG;i++) {

    for(int j=1;j<=n;j++) {

      baba[i][j]=baba[i-1][baba[i-1][j]];

    }

  }

}

void dfs1(int node,int ata) {

  baba[0][node]=ata;
  der[node]=der[ata]+1;

  for(int i:v[node]) {

    if(i==ata) continue ;

    dfs1(i,node);

  }

}

int main() {
  
srand(time(NULL));

//  freopen("in.txt","r",stdin);

  scanf("%d ",&n);

  for(int i=1;i<n;i++) {

    scanf("%d %d",&x,&y);

    v[x].pb(y);
    v[y].pb(x);

  }

  dfs1(1,0);
  build_lca();

  scanf("%d",&q);

  while(q--) {

    char c;

    scanf(" %c %d %d %d",&c,&x,&y,&z);

    if(c=='M') {

      Max.pb({z,{x,y}});      

    }
    else {

      Min.pb({z,{x,y}});

    }

  }

  sort(all(Max),greater<iii>());
  sort(all(Min));

    for(auto x:Max) {

      int n1=x.nd.st;
      int n2=x.nd.nd;
      int cst=x.st;

      add[n1].insert(cst);
      add[n2].insert(cst);

      del[lca(n1,n2)].pb(cst);

    }

    for(auto x:Min) {

      int n1=x.nd.st;
      int n2=x.nd.nd;
      int cst=x.st;

      add[n1].insert(cst);
      add[n2].insert(cst);

      del[lca(n1,n2)].pb(cst);

    }

    dfs3(1,0);

    for(int i=2;i<=n;i++) {

      int rnd=rand()%2;

      if(!sz(Min)) rnd=1;

      printf("%d %d %d\n",i,baba[0][i],rnd?xcost[i]:ncost[i]);

    }

    return 0;

  

  assert(0);

}

Compilation message

minmaxtree.cpp: In function 'int main()':
minmaxtree.cpp:121:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d ",&n);
   ~~~~~^~~~~~~~~~
minmaxtree.cpp:125:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d",&x,&y);
     ~~~~~^~~~~~~~~~~~~~~
minmaxtree.cpp:135:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&q);
   ~~~~~^~~~~~~~~
minmaxtree.cpp:141:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf(" %c %d %d %d",&c,&x,&y,&z);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 8 ms 7032 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 401 ms 30696 KB Output is correct
2 Correct 494 ms 30696 KB Output is correct
3 Correct 291 ms 30696 KB Output is correct
4 Correct 374 ms 32480 KB Output is correct
5 Correct 296 ms 32480 KB Output is correct
6 Correct 421 ms 32480 KB Output is correct
7 Correct 335 ms 32480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 241 ms 32480 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 8 ms 7032 KB Output isn't correct
2 Halted 0 ms 0 KB -