답안 #122023

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
122023 2019-06-27T11:40:08 Z KLPP Designated Cities (JOI19_designated_cities) C++14
23 / 100
2000 ms 75652 KB
#include<bits/stdc++.h>

using namespace std;
typedef long long int lld;
typedef pair<lld,lld> pii;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define trav(a,v) for(auto a:v)
vector<pii> nei[200000];
vector<pii> inv[200000];
set<int> leaves;
map<int,lld> tree[200000];
int n;
vector<int> V;
void correct(){
  rep(i,0,n){
    if(tree[i].size()==2){
      //cout<<i<<endl;
      trav(x,tree[i]){
	V.push_back(x.first);
      }
      //cout<<V[0]<<" "<<V[1]<<endl;
      lld go=tree[V[0]][i]+tree[i][V[1]];
      lld come=tree[V[1]][i]+tree[i][V[0]];
      tree[V[0]].erase(tree[V[0]].find(i));
      tree[V[1]].erase(tree[V[1]].find(i));
      tree[V[0]][V[1]]=go;
      tree[V[1]][V[0]]=come;
      V.clear();
      tree[i].clear();
    }
  }
}
int view(){
  pii best=pii(1000000000000000,-1);
  trav(l,leaves){
    //cout<<l<<endl;
    pii travel=pii(-1,l);
    lld ans=0;
    while(tree[travel.second].size()==2 || travel.first==-1){
      //if(travel.first==1)cout<<travel.first<<" "<<travel.second<<" "<<tree[travel.second].size()<<endl;
      trav(u,tree[travel.second]){
	if(travel.first!=u.first){
	  travel.first=travel.second;
	  travel.second=u.first;
	  ans+=u.second;
	}
      }
    }
    best=min(best,pii(ans,l));
    //cout<<ans<<" "<<l<<endl;
  }
  return best.second;
}

bool visited[200000];
lld size[200000];
lld size2[200000];
void DFS(int node){
  visited[node]=true;
  size[node]=0;
  trav(p,nei[node]){
    if(!visited[p.first]){
      DFS(p.first);
      size[node]+=size[p.first]+p.second;
      size2[p.first]=-p.second;
    }
  }
}
 
void DFS2(int node){
  visited[node]=true;
  trav(p,inv[node]){
    if(!visited[p.first]){
      //cout<<node<<" "<<p.first<<" "<<p.second<<endl;
      size2[p.first]+=size2[node]+p.second;
      DFS2(p.first);
    }
  }
}
void print(){
  rep(i,0,n){
    trav(p,tree[i]){
      cout<<p.first<<","<<p.second<<" ";
    }
    cout<<endl;
  }
}
int main(){
  scanf("%d",&n);
  rep(i,0,n-1){
    int x,y;
    lld z,w;
    scanf("%d %d %lld %lld",&x,&y,&z,&w);
    x--;y--;
    tree[x][y]=w;
    tree[y][x]=z;
    nei[x].push_back(pii(y,z));
    nei[y].push_back(pii(x,w));
    inv[x].push_back(pii(y,w));
    inv[y].push_back(pii(x,z));
  }
  
  rep(i,0,n){
    if(tree[i].size()==1){
      leaves.insert(i);
    }
  }
  lld answer[n+1];
  rep(i,leaves.size(),n){
    answer[i]=0;
  }
  
  correct();
  lld ans=0;
  while(leaves.size()>2){
    int u=view();
    pii p=*tree[u].begin();
    tree[u].clear();
    ans+=p.second;
    tree[p.first].erase(tree[p.first].find(u));
    correct();
    //cout<<tree[0].size()<<endl;
    //print();
    leaves.erase(u);
    answer[leaves.size()]=ans;
    //cout<<ans<<endl;
  }
  rep(i,0,n)visited[i]=false;
  DFS(0);
  size2[0]=size[0];
  rep(i,0,n)visited[i]=false;
  DFS2(0);
  answer[1]=size2[0];
  rep(i,0,n)answer[1]=min(answer[1],size2[i]);
  int q;
  scanf("%d",&q);
  while(q--){
    int x;
    scanf("%d",&x);
    printf("%lld\n",answer[x]);
  }
  
  return 0;
}

Compilation message

designated_cities.cpp: In function 'int main()':
designated_cities.cpp:89:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&n);
   ~~~~~^~~~~~~~~
designated_cities.cpp:93:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d %lld %lld",&x,&y,&z,&w);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:136:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&q);
   ~~~~~^~~~~~~~~
designated_cities.cpp:139:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&x);
     ~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 19200 KB Output is correct
2 Correct 19 ms 19200 KB Output is correct
3 Correct 18 ms 19072 KB Output is correct
4 Correct 18 ms 19200 KB Output is correct
5 Correct 17 ms 19072 KB Output is correct
6 Correct 19 ms 19072 KB Output is correct
7 Correct 18 ms 19072 KB Output is correct
8 Correct 18 ms 19192 KB Output is correct
9 Correct 18 ms 19192 KB Output is correct
10 Correct 19 ms 19192 KB Output is correct
11 Correct 17 ms 19200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 19200 KB Output is correct
2 Execution timed out 2047 ms 73432 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 19072 KB Output is correct
2 Execution timed out 2013 ms 75652 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 19200 KB Output is correct
2 Correct 19 ms 19200 KB Output is correct
3 Correct 18 ms 19072 KB Output is correct
4 Correct 18 ms 19200 KB Output is correct
5 Correct 17 ms 19072 KB Output is correct
6 Correct 19 ms 19072 KB Output is correct
7 Correct 18 ms 19072 KB Output is correct
8 Correct 18 ms 19192 KB Output is correct
9 Correct 18 ms 19192 KB Output is correct
10 Correct 19 ms 19192 KB Output is correct
11 Correct 17 ms 19200 KB Output is correct
12 Correct 19 ms 19108 KB Output is correct
13 Correct 32 ms 19728 KB Output is correct
14 Correct 22 ms 19784 KB Output is correct
15 Correct 32 ms 19684 KB Output is correct
16 Correct 32 ms 19812 KB Output is correct
17 Correct 35 ms 19696 KB Output is correct
18 Correct 35 ms 19804 KB Output is correct
19 Correct 32 ms 19716 KB Output is correct
20 Correct 34 ms 19704 KB Output is correct
21 Correct 31 ms 19720 KB Output is correct
22 Correct 29 ms 19732 KB Output is correct
23 Correct 35 ms 19724 KB Output is correct
24 Correct 66 ms 19732 KB Output is correct
25 Correct 23 ms 19804 KB Output is correct
26 Correct 72 ms 19784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 19200 KB Output is correct
2 Execution timed out 2047 ms 73432 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 19200 KB Output is correct
2 Correct 19 ms 19200 KB Output is correct
3 Correct 18 ms 19072 KB Output is correct
4 Correct 18 ms 19200 KB Output is correct
5 Correct 17 ms 19072 KB Output is correct
6 Correct 19 ms 19072 KB Output is correct
7 Correct 18 ms 19072 KB Output is correct
8 Correct 18 ms 19192 KB Output is correct
9 Correct 18 ms 19192 KB Output is correct
10 Correct 19 ms 19192 KB Output is correct
11 Correct 17 ms 19200 KB Output is correct
12 Correct 17 ms 19200 KB Output is correct
13 Execution timed out 2047 ms 73432 KB Time limit exceeded
14 Halted 0 ms 0 KB -