Submission #122034

# Submission time Handle Problem Language Result Execution time Memory
122034 2019-06-27T12:02:31 Z KLPP Designated Cities (JOI19_designated_cities) C++14
100 / 100
1830 ms 119480 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)
#define INF 1000000000000000000
vector<pii> nei[200000];
vector<pii> inv[200000];
set<int> leaves;
map<int,lld> tree[200000];
int n;
vector<int> V;
class ST{
  pii arr[1000000];
public:
  void build(int a, int b, int node){
    if(a==b){
      arr[node]=pii(0,a);
      return;
    }
    int mid=(a+b)/2;
    build(a,mid,2*node);
    build(mid+1,b,2*node+1);
    arr[node]=min(arr[2*node],arr[2*node+1]);
  }
  void update(int a, int b, int node, int pos, lld val){
    if(pos<a || b<pos)return;
    if(a==b){
      arr[node]=pii(val,pos);
      return;
    }
    int mid=(a+b)/2;
    update(a,mid,2*node,pos,val);
    update(mid+1,b,2*node+1,pos,val);
    arr[node]=min(arr[2*node],arr[2*node+1]);
  }
  int query(){
    return arr[1].second;
  }
  int query2(){
    return arr[1].first;
  }
};

ST *DEG;
ST *candidates;
void correct(){
  while(DEG->query2()==2){
    int i=DEG->query();
      //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;
      DEG->update(0,n-1,1,i,INF);
      
      V.clear();
      tree[i].clear();
      if(leaves.find(V[0])!=leaves.end()){
	candidates->update(0,n-1,1,V[0],go);
      }
      if(leaves.find(V[1])!=leaves.end()){
	candidates->update(0,n-1,1,V[1],come);
      }
  }
}
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));
  }
  DEG=new ST();
  candidates=new ST();
  DEG->build(0,n-1,1);
  candidates->build(0,n-1,1);
  rep(i,0,n){
    if(tree[i].size()==1){
      leaves.insert(i);
      candidates->update(0,n-1,1,i,tree[i].begin()->second);
      DEG->update(0,n-1,1,i,INF);
    }else{
      candidates->update(0,n-1,1,i,INF);
      DEG->update(0,n-1,1,i,tree[i].size());
    }
  }
  lld answer[n+1];
  rep(i,leaves.size(),n){
    answer[i]=0;
  }
  
  correct();
  lld ans=0;
  while(leaves.size()>2){
    int u=candidates->query();
    pii p=*tree[u].begin();
    tree[u].clear();
    ans+=p.second;
    tree[p.first].erase(tree[p.first].find(u));
    DEG->update(0,n-1,1,p.first,tree[p.first].size());
    correct();
    //cout<<tree[0].size()<<endl;
    //print();
    leaves.erase(u);
    candidates->update(0,n-1,1,u,INF);
    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:131:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&n);
   ~~~~~^~~~~~~~~
designated_cities.cpp:135: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:188:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&q);
   ~~~~~^~~~~~~~~
designated_cities.cpp:191:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&x);
     ~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 42 ms 50424 KB Output is correct
2 Correct 41 ms 50424 KB Output is correct
3 Correct 40 ms 50424 KB Output is correct
4 Correct 41 ms 50424 KB Output is correct
5 Correct 40 ms 50396 KB Output is correct
6 Correct 40 ms 50424 KB Output is correct
7 Correct 41 ms 50424 KB Output is correct
8 Correct 42 ms 50428 KB Output is correct
9 Correct 40 ms 50424 KB Output is correct
10 Correct 42 ms 50492 KB Output is correct
11 Correct 40 ms 50428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 50424 KB Output is correct
2 Correct 1409 ms 105176 KB Output is correct
3 Correct 1129 ms 112360 KB Output is correct
4 Correct 1349 ms 109168 KB Output is correct
5 Correct 1514 ms 109380 KB Output is correct
6 Correct 1462 ms 110412 KB Output is correct
7 Correct 1628 ms 109792 KB Output is correct
8 Correct 1287 ms 113776 KB Output is correct
9 Correct 1546 ms 112840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 50432 KB Output is correct
2 Correct 1460 ms 104880 KB Output is correct
3 Correct 1103 ms 115928 KB Output is correct
4 Correct 1345 ms 109792 KB Output is correct
5 Correct 1520 ms 111608 KB Output is correct
6 Correct 1503 ms 112860 KB Output is correct
7 Correct 1568 ms 114116 KB Output is correct
8 Correct 1428 ms 115844 KB Output is correct
9 Correct 1549 ms 114764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 50424 KB Output is correct
2 Correct 41 ms 50424 KB Output is correct
3 Correct 40 ms 50424 KB Output is correct
4 Correct 41 ms 50424 KB Output is correct
5 Correct 40 ms 50396 KB Output is correct
6 Correct 40 ms 50424 KB Output is correct
7 Correct 41 ms 50424 KB Output is correct
8 Correct 42 ms 50428 KB Output is correct
9 Correct 40 ms 50424 KB Output is correct
10 Correct 42 ms 50492 KB Output is correct
11 Correct 40 ms 50428 KB Output is correct
12 Correct 42 ms 50424 KB Output is correct
13 Correct 48 ms 51064 KB Output is correct
14 Correct 48 ms 51064 KB Output is correct
15 Correct 48 ms 51064 KB Output is correct
16 Correct 48 ms 51064 KB Output is correct
17 Correct 49 ms 51192 KB Output is correct
18 Correct 50 ms 51068 KB Output is correct
19 Correct 52 ms 51076 KB Output is correct
20 Correct 51 ms 51068 KB Output is correct
21 Correct 45 ms 51064 KB Output is correct
22 Correct 45 ms 51064 KB Output is correct
23 Correct 46 ms 51064 KB Output is correct
24 Correct 46 ms 51064 KB Output is correct
25 Correct 47 ms 51064 KB Output is correct
26 Correct 45 ms 51064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 50424 KB Output is correct
2 Correct 1409 ms 105176 KB Output is correct
3 Correct 1129 ms 112360 KB Output is correct
4 Correct 1349 ms 109168 KB Output is correct
5 Correct 1514 ms 109380 KB Output is correct
6 Correct 1462 ms 110412 KB Output is correct
7 Correct 1628 ms 109792 KB Output is correct
8 Correct 1287 ms 113776 KB Output is correct
9 Correct 1546 ms 112840 KB Output is correct
10 Correct 41 ms 50432 KB Output is correct
11 Correct 1460 ms 104880 KB Output is correct
12 Correct 1103 ms 115928 KB Output is correct
13 Correct 1345 ms 109792 KB Output is correct
14 Correct 1520 ms 111608 KB Output is correct
15 Correct 1503 ms 112860 KB Output is correct
16 Correct 1568 ms 114116 KB Output is correct
17 Correct 1428 ms 115844 KB Output is correct
18 Correct 1549 ms 114764 KB Output is correct
19 Correct 40 ms 50424 KB Output is correct
20 Correct 1459 ms 111108 KB Output is correct
21 Correct 1132 ms 116152 KB Output is correct
22 Correct 1567 ms 109756 KB Output is correct
23 Correct 1507 ms 111716 KB Output is correct
24 Correct 1556 ms 110724 KB Output is correct
25 Correct 1539 ms 111704 KB Output is correct
26 Correct 1522 ms 110608 KB Output is correct
27 Correct 1551 ms 111672 KB Output is correct
28 Correct 1527 ms 112772 KB Output is correct
29 Correct 1453 ms 111676 KB Output is correct
30 Correct 1448 ms 110920 KB Output is correct
31 Correct 1543 ms 112724 KB Output is correct
32 Correct 1387 ms 116088 KB Output is correct
33 Correct 1471 ms 115284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 50424 KB Output is correct
2 Correct 41 ms 50424 KB Output is correct
3 Correct 40 ms 50424 KB Output is correct
4 Correct 41 ms 50424 KB Output is correct
5 Correct 40 ms 50396 KB Output is correct
6 Correct 40 ms 50424 KB Output is correct
7 Correct 41 ms 50424 KB Output is correct
8 Correct 42 ms 50428 KB Output is correct
9 Correct 40 ms 50424 KB Output is correct
10 Correct 42 ms 50492 KB Output is correct
11 Correct 40 ms 50428 KB Output is correct
12 Correct 40 ms 50424 KB Output is correct
13 Correct 1409 ms 105176 KB Output is correct
14 Correct 1129 ms 112360 KB Output is correct
15 Correct 1349 ms 109168 KB Output is correct
16 Correct 1514 ms 109380 KB Output is correct
17 Correct 1462 ms 110412 KB Output is correct
18 Correct 1628 ms 109792 KB Output is correct
19 Correct 1287 ms 113776 KB Output is correct
20 Correct 1546 ms 112840 KB Output is correct
21 Correct 41 ms 50432 KB Output is correct
22 Correct 1460 ms 104880 KB Output is correct
23 Correct 1103 ms 115928 KB Output is correct
24 Correct 1345 ms 109792 KB Output is correct
25 Correct 1520 ms 111608 KB Output is correct
26 Correct 1503 ms 112860 KB Output is correct
27 Correct 1568 ms 114116 KB Output is correct
28 Correct 1428 ms 115844 KB Output is correct
29 Correct 1549 ms 114764 KB Output is correct
30 Correct 42 ms 50424 KB Output is correct
31 Correct 48 ms 51064 KB Output is correct
32 Correct 48 ms 51064 KB Output is correct
33 Correct 48 ms 51064 KB Output is correct
34 Correct 48 ms 51064 KB Output is correct
35 Correct 49 ms 51192 KB Output is correct
36 Correct 50 ms 51068 KB Output is correct
37 Correct 52 ms 51076 KB Output is correct
38 Correct 51 ms 51068 KB Output is correct
39 Correct 45 ms 51064 KB Output is correct
40 Correct 45 ms 51064 KB Output is correct
41 Correct 46 ms 51064 KB Output is correct
42 Correct 46 ms 51064 KB Output is correct
43 Correct 47 ms 51064 KB Output is correct
44 Correct 45 ms 51064 KB Output is correct
45 Correct 40 ms 50424 KB Output is correct
46 Correct 1459 ms 111108 KB Output is correct
47 Correct 1132 ms 116152 KB Output is correct
48 Correct 1567 ms 109756 KB Output is correct
49 Correct 1507 ms 111716 KB Output is correct
50 Correct 1556 ms 110724 KB Output is correct
51 Correct 1539 ms 111704 KB Output is correct
52 Correct 1522 ms 110608 KB Output is correct
53 Correct 1551 ms 111672 KB Output is correct
54 Correct 1527 ms 112772 KB Output is correct
55 Correct 1453 ms 111676 KB Output is correct
56 Correct 1448 ms 110920 KB Output is correct
57 Correct 1543 ms 112724 KB Output is correct
58 Correct 1387 ms 116088 KB Output is correct
59 Correct 1471 ms 115284 KB Output is correct
60 Correct 46 ms 50444 KB Output is correct
61 Correct 1492 ms 113880 KB Output is correct
62 Correct 1168 ms 116472 KB Output is correct
63 Correct 1467 ms 112444 KB Output is correct
64 Correct 1480 ms 114456 KB Output is correct
65 Correct 1485 ms 112944 KB Output is correct
66 Correct 1494 ms 114296 KB Output is correct
67 Correct 1532 ms 112972 KB Output is correct
68 Correct 1609 ms 114388 KB Output is correct
69 Correct 1527 ms 115280 KB Output is correct
70 Correct 1509 ms 113784 KB Output is correct
71 Correct 1830 ms 113572 KB Output is correct
72 Correct 1585 ms 115568 KB Output is correct
73 Correct 1487 ms 117664 KB Output is correct
74 Correct 1545 ms 119480 KB Output is correct