답안 #140113

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
140113 2019-08-02T05:53:01 Z KLPP Mergers (JOI19_mergers) C++14
70 / 100
3000 ms 188336 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
int n,k;
vector<int> nei[1000000];
int state[1000000];
vector<int> states[1000000];
int dfs_num[1000000];
int dfs_low[1000000];
stack<int> s;
int parent[1000000];
int counter;
int SCC_count;
int SCC[1000000];
int deg[1000000];
bool visited[1000000];
void DFS(int node){
  dfs_num[node]=counter;
  dfs_low[node]=counter;
  counter++;
  s.push(node);
  visited[node]=true;
  trav(a,nei[node]){
    if(dfs_num[a]==-1){
      parent[a]=node;
      DFS(a);
      dfs_low[node]=min(dfs_low[node],dfs_low[a]);
    }else{
      if(parent[node]!=a && visited[a]){
	dfs_low[node]=min(dfs_low[node],dfs_num[a]);
      }
    }
  }
  if(dfs_low[node]==dfs_num[node]){
    while(true){
      int u=s.top();
      s.pop();
      visited[u]=false;
      SCC[u]=SCC_count;
      dfs_num[u]=-1;
      if(u==node){
	SCC_count++;
	return;
      }
    }
  }
}
class DSU{
  int parent[1000000];
  int size[1000000];
public:
  void init(int K){
    rep(i,0,K){
      parent[i]=i;
      size[i]=1;
    }
  }
  int root(int node){
    if(parent[node]==node)return node;
    parent[node]=root(parent[node]);
    return parent[node];
  }
  void merge(int a, int b){
    a=root(a);
    b=root(b);
    if(a==b)return;
    if(size[a]<size[b])swap(a,b);
    size[a]+=size[b];
    parent[b]=a;
  }
};
int main(){
  scanf("%d %d",&n,&k);
  set<pii> edges;
  rep(i,0,n-1){
    int x,y;
    scanf("%d %d",&x,&y);
    x--;y--;
    nei[x].push_back(y);
    nei[y].push_back(x);
    edges.insert(pii(x,y));
    edges.insert(pii(y,x));
  }
  rep(i,0,n){
    scanf("%d",&state[i]);
    state[i]--;
    states[state[i]].push_back(i);
  }
  rep(i,0,k){
    rep(j,0,states[i].size()-1){
      pii new_edge=pii(states[i][j],states[i][j+1]);
      if(edges.find(new_edge)==edges.end()){
	nei[states[i][j]].push_back(states[i][j+1]);
	nei[states[i][j+1]].push_back(states[i][j]);
      }
      //edges.push_back(pii(states[i][j],states[i][j+1]));
    }
  }
  /*bool important[edges.size()];
  rep(i,0,edges.size())important[i]=true;
  rep(i,0,edges.size()){
    rep(j,0,n){
      visited[j]=false;
      nei[j].clear();
    }
    rep(j,0,edges.size()){
      if(j!=i){
	nei[edges[j].first].push_back(edges[j].second);
	nei[edges[j].second].push_back(edges[j].first);
      }
    }
    DFS2(0);
    rep(j,0,n){
      
      important[i]=important[i]&visited[j];
    }
  }*/
  /*rep(i,0,n-1)cout<<important[i];
  cout<<endl;*/
  /*rep(i,0,edges.size()){
    if(important[i]){
      nei[edges[i].first].push_back(edges[i].second);
      nei[edges[i].second].push_back(edges[i].first);
    }
  }
  rep(i,0,n){
    trav(a,nei[i])cout<<a<<" ";
    cout<<endl;
  }cout<<endl;*/
  rep(i,0,n){
    dfs_low[i]=-1;
    dfs_num[i]=-1;
    visited[i]=false;
  }
  counter=0;
  SCC_count=0;
  DFS(0);
  //rep(i,0,n)cout<<SCC[i]<<endl;
  
  /*rep(i,0,n){
    visited[i]=false;
  }*/
  /*SCC_count=0;
  rep(i,0,n){
    if(!visited[i]){
      DFS2(i);
      while(!s.empty()){
	SCC[s.top()]=SCC_count;
	s.pop();
      }
      SCC_count++;
    }
    }*/
  //rep(i,0,n)cout<<SCC[i]<<endl;
  DSU *D=new DSU();
  D->init(SCC_count);
  rep(i,0,k){
    rep(j,0,states[i].size()-1){
      pii new_edge=pii(states[i][j],states[i][j+1]);
      if(edges.find(new_edge)!=edges.end()){
	D->merge(SCC[new_edge.first],SCC[new_edge.second]);
      }
      //edges.push_back(pii(states[i][j],states[i][j+1]));
    }
  }
  rep(i,0,n)deg[i]=0;
  trav(a,edges){
    a.first=D->root(SCC[a.first]);
    a.second=D->root(SCC[a.second]);
    if(a.first!=a.second){
      //cout<<a.first<<" "<<a.second<<endl;
      deg[a.first]++;
      deg[a.second]++;
    }
  }
  int leaves=0;
  rep(i,0,SCC_count){
    if(deg[i]==2)leaves++;
  }
  printf("%d\n",(leaves+1)/2);
  return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:6:33: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define rep(i,a,b) for(int i=a;i<b;i++)
mergers.cpp:95:9:
     rep(j,0,states[i].size()-1){
         ~~~~~~~~~~~~~~~~~~~~~~   
mergers.cpp:95:5: note: in expansion of macro 'rep'
     rep(j,0,states[i].size()-1){
     ^~~
mergers.cpp:6:33: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define rep(i,a,b) for(int i=a;i<b;i++)
mergers.cpp:163:9:
     rep(j,0,states[i].size()-1){
         ~~~~~~~~~~~~~~~~~~~~~~   
mergers.cpp:163:5: note: in expansion of macro 'rep'
     rep(j,0,states[i].size()-1){
     ^~~
mergers.cpp:78:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&n,&k);
   ~~~~~^~~~~~~~~~~~~~~
mergers.cpp:82:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d",&x,&y);
     ~~~~~^~~~~~~~~~~~~~~
mergers.cpp:90:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&state[i]);
     ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 55160 KB Output is correct
2 Correct 52 ms 55288 KB Output is correct
3 Correct 52 ms 55288 KB Output is correct
4 Correct 53 ms 55160 KB Output is correct
5 Correct 52 ms 55100 KB Output is correct
6 Correct 52 ms 55160 KB Output is correct
7 Correct 52 ms 55160 KB Output is correct
8 Correct 52 ms 55288 KB Output is correct
9 Correct 53 ms 55160 KB Output is correct
10 Correct 67 ms 55160 KB Output is correct
11 Correct 52 ms 55160 KB Output is correct
12 Correct 52 ms 55160 KB Output is correct
13 Correct 52 ms 55160 KB Output is correct
14 Correct 52 ms 55288 KB Output is correct
15 Correct 53 ms 55176 KB Output is correct
16 Correct 53 ms 55160 KB Output is correct
17 Correct 53 ms 55160 KB Output is correct
18 Correct 53 ms 55288 KB Output is correct
19 Correct 53 ms 55160 KB Output is correct
20 Correct 53 ms 55160 KB Output is correct
21 Correct 52 ms 55160 KB Output is correct
22 Correct 53 ms 55288 KB Output is correct
23 Correct 56 ms 55160 KB Output is correct
24 Correct 52 ms 55160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 55160 KB Output is correct
2 Correct 52 ms 55288 KB Output is correct
3 Correct 52 ms 55288 KB Output is correct
4 Correct 53 ms 55160 KB Output is correct
5 Correct 52 ms 55100 KB Output is correct
6 Correct 52 ms 55160 KB Output is correct
7 Correct 52 ms 55160 KB Output is correct
8 Correct 52 ms 55288 KB Output is correct
9 Correct 53 ms 55160 KB Output is correct
10 Correct 67 ms 55160 KB Output is correct
11 Correct 52 ms 55160 KB Output is correct
12 Correct 52 ms 55160 KB Output is correct
13 Correct 52 ms 55160 KB Output is correct
14 Correct 52 ms 55288 KB Output is correct
15 Correct 53 ms 55176 KB Output is correct
16 Correct 53 ms 55160 KB Output is correct
17 Correct 53 ms 55160 KB Output is correct
18 Correct 53 ms 55288 KB Output is correct
19 Correct 53 ms 55160 KB Output is correct
20 Correct 53 ms 55160 KB Output is correct
21 Correct 52 ms 55160 KB Output is correct
22 Correct 53 ms 55288 KB Output is correct
23 Correct 56 ms 55160 KB Output is correct
24 Correct 52 ms 55160 KB Output is correct
25 Correct 53 ms 55136 KB Output is correct
26 Correct 58 ms 55800 KB Output is correct
27 Correct 58 ms 55800 KB Output is correct
28 Correct 62 ms 55928 KB Output is correct
29 Correct 57 ms 55900 KB Output is correct
30 Correct 58 ms 55968 KB Output is correct
31 Correct 52 ms 55160 KB Output is correct
32 Correct 57 ms 56056 KB Output is correct
33 Correct 53 ms 55160 KB Output is correct
34 Correct 57 ms 55800 KB Output is correct
35 Correct 57 ms 55804 KB Output is correct
36 Correct 58 ms 55800 KB Output is correct
37 Correct 58 ms 55800 KB Output is correct
38 Correct 53 ms 55288 KB Output is correct
39 Correct 57 ms 55928 KB Output is correct
40 Correct 58 ms 55928 KB Output is correct
41 Correct 81 ms 55928 KB Output is correct
42 Correct 57 ms 55928 KB Output is correct
43 Correct 58 ms 56056 KB Output is correct
44 Correct 52 ms 55160 KB Output is correct
45 Correct 57 ms 55928 KB Output is correct
46 Correct 58 ms 55800 KB Output is correct
47 Correct 53 ms 55292 KB Output is correct
48 Correct 58 ms 55800 KB Output is correct
49 Correct 68 ms 55900 KB Output is correct
50 Correct 66 ms 55928 KB Output is correct
51 Correct 59 ms 55816 KB Output is correct
52 Correct 58 ms 55800 KB Output is correct
53 Correct 58 ms 55800 KB Output is correct
54 Correct 60 ms 55928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 55160 KB Output is correct
2 Correct 52 ms 55288 KB Output is correct
3 Correct 52 ms 55288 KB Output is correct
4 Correct 53 ms 55160 KB Output is correct
5 Correct 52 ms 55100 KB Output is correct
6 Correct 52 ms 55160 KB Output is correct
7 Correct 52 ms 55160 KB Output is correct
8 Correct 52 ms 55288 KB Output is correct
9 Correct 53 ms 55160 KB Output is correct
10 Correct 67 ms 55160 KB Output is correct
11 Correct 52 ms 55160 KB Output is correct
12 Correct 52 ms 55160 KB Output is correct
13 Correct 52 ms 55160 KB Output is correct
14 Correct 52 ms 55288 KB Output is correct
15 Correct 53 ms 55176 KB Output is correct
16 Correct 53 ms 55160 KB Output is correct
17 Correct 53 ms 55160 KB Output is correct
18 Correct 53 ms 55288 KB Output is correct
19 Correct 53 ms 55160 KB Output is correct
20 Correct 53 ms 55160 KB Output is correct
21 Correct 52 ms 55160 KB Output is correct
22 Correct 53 ms 55288 KB Output is correct
23 Correct 56 ms 55160 KB Output is correct
24 Correct 52 ms 55160 KB Output is correct
25 Correct 53 ms 55204 KB Output is correct
26 Correct 530 ms 76264 KB Output is correct
27 Correct 542 ms 80836 KB Output is correct
28 Correct 59 ms 55800 KB Output is correct
29 Correct 52 ms 55160 KB Output is correct
30 Correct 53 ms 55288 KB Output is correct
31 Correct 551 ms 76908 KB Output is correct
32 Correct 58 ms 55928 KB Output is correct
33 Correct 547 ms 81836 KB Output is correct
34 Correct 564 ms 80248 KB Output is correct
35 Correct 59 ms 55800 KB Output is correct
36 Correct 524 ms 76252 KB Output is correct
37 Correct 58 ms 55928 KB Output is correct
38 Correct 58 ms 55800 KB Output is correct
39 Correct 502 ms 76052 KB Output is correct
40 Correct 58 ms 55928 KB Output is correct
41 Correct 485 ms 80788 KB Output is correct
42 Correct 536 ms 81860 KB Output is correct
43 Correct 53 ms 55416 KB Output is correct
44 Correct 482 ms 81900 KB Output is correct
45 Correct 658 ms 82288 KB Output is correct
46 Correct 58 ms 55800 KB Output is correct
47 Correct 58 ms 55928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 476 ms 74776 KB Output is correct
2 Correct 337 ms 76868 KB Output is correct
3 Correct 57 ms 55800 KB Output is correct
4 Correct 70 ms 55800 KB Output is correct
5 Correct 53 ms 55160 KB Output is correct
6 Correct 52 ms 55288 KB Output is correct
7 Correct 58 ms 55800 KB Output is correct
8 Correct 508 ms 77048 KB Output is correct
9 Correct 58 ms 55804 KB Output is correct
10 Correct 581 ms 76384 KB Output is correct
11 Correct 53 ms 55160 KB Output is correct
12 Correct 559 ms 75680 KB Output is correct
13 Correct 516 ms 76960 KB Output is correct
14 Correct 392 ms 78360 KB Output is correct
15 Correct 496 ms 76160 KB Output is correct
16 Correct 57 ms 55800 KB Output is correct
17 Correct 53 ms 55288 KB Output is correct
18 Correct 392 ms 78448 KB Output is correct
19 Correct 460 ms 81700 KB Output is correct
20 Correct 70 ms 55800 KB Output is correct
21 Correct 55 ms 55388 KB Output is correct
22 Correct 454 ms 77296 KB Output is correct
23 Correct 59 ms 55772 KB Output is correct
24 Correct 541 ms 76408 KB Output is correct
25 Correct 435 ms 80504 KB Output is correct
26 Correct 73 ms 55928 KB Output is correct
27 Correct 58 ms 55912 KB Output is correct
28 Correct 58 ms 55800 KB Output is correct
29 Correct 58 ms 55804 KB Output is correct
30 Correct 58 ms 55772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 55160 KB Output is correct
2 Correct 52 ms 55288 KB Output is correct
3 Correct 52 ms 55288 KB Output is correct
4 Correct 53 ms 55160 KB Output is correct
5 Correct 52 ms 55100 KB Output is correct
6 Correct 52 ms 55160 KB Output is correct
7 Correct 52 ms 55160 KB Output is correct
8 Correct 52 ms 55288 KB Output is correct
9 Correct 53 ms 55160 KB Output is correct
10 Correct 67 ms 55160 KB Output is correct
11 Correct 52 ms 55160 KB Output is correct
12 Correct 52 ms 55160 KB Output is correct
13 Correct 52 ms 55160 KB Output is correct
14 Correct 52 ms 55288 KB Output is correct
15 Correct 53 ms 55176 KB Output is correct
16 Correct 53 ms 55160 KB Output is correct
17 Correct 53 ms 55160 KB Output is correct
18 Correct 53 ms 55288 KB Output is correct
19 Correct 53 ms 55160 KB Output is correct
20 Correct 53 ms 55160 KB Output is correct
21 Correct 52 ms 55160 KB Output is correct
22 Correct 53 ms 55288 KB Output is correct
23 Correct 56 ms 55160 KB Output is correct
24 Correct 52 ms 55160 KB Output is correct
25 Correct 53 ms 55136 KB Output is correct
26 Correct 58 ms 55800 KB Output is correct
27 Correct 58 ms 55800 KB Output is correct
28 Correct 62 ms 55928 KB Output is correct
29 Correct 57 ms 55900 KB Output is correct
30 Correct 58 ms 55968 KB Output is correct
31 Correct 52 ms 55160 KB Output is correct
32 Correct 57 ms 56056 KB Output is correct
33 Correct 53 ms 55160 KB Output is correct
34 Correct 57 ms 55800 KB Output is correct
35 Correct 57 ms 55804 KB Output is correct
36 Correct 58 ms 55800 KB Output is correct
37 Correct 58 ms 55800 KB Output is correct
38 Correct 53 ms 55288 KB Output is correct
39 Correct 57 ms 55928 KB Output is correct
40 Correct 58 ms 55928 KB Output is correct
41 Correct 81 ms 55928 KB Output is correct
42 Correct 57 ms 55928 KB Output is correct
43 Correct 58 ms 56056 KB Output is correct
44 Correct 52 ms 55160 KB Output is correct
45 Correct 57 ms 55928 KB Output is correct
46 Correct 58 ms 55800 KB Output is correct
47 Correct 53 ms 55292 KB Output is correct
48 Correct 58 ms 55800 KB Output is correct
49 Correct 68 ms 55900 KB Output is correct
50 Correct 66 ms 55928 KB Output is correct
51 Correct 59 ms 55816 KB Output is correct
52 Correct 58 ms 55800 KB Output is correct
53 Correct 58 ms 55800 KB Output is correct
54 Correct 60 ms 55928 KB Output is correct
55 Correct 53 ms 55204 KB Output is correct
56 Correct 530 ms 76264 KB Output is correct
57 Correct 542 ms 80836 KB Output is correct
58 Correct 59 ms 55800 KB Output is correct
59 Correct 52 ms 55160 KB Output is correct
60 Correct 53 ms 55288 KB Output is correct
61 Correct 551 ms 76908 KB Output is correct
62 Correct 58 ms 55928 KB Output is correct
63 Correct 547 ms 81836 KB Output is correct
64 Correct 564 ms 80248 KB Output is correct
65 Correct 59 ms 55800 KB Output is correct
66 Correct 524 ms 76252 KB Output is correct
67 Correct 58 ms 55928 KB Output is correct
68 Correct 58 ms 55800 KB Output is correct
69 Correct 502 ms 76052 KB Output is correct
70 Correct 58 ms 55928 KB Output is correct
71 Correct 485 ms 80788 KB Output is correct
72 Correct 536 ms 81860 KB Output is correct
73 Correct 53 ms 55416 KB Output is correct
74 Correct 482 ms 81900 KB Output is correct
75 Correct 658 ms 82288 KB Output is correct
76 Correct 58 ms 55800 KB Output is correct
77 Correct 58 ms 55928 KB Output is correct
78 Correct 476 ms 74776 KB Output is correct
79 Correct 337 ms 76868 KB Output is correct
80 Correct 57 ms 55800 KB Output is correct
81 Correct 70 ms 55800 KB Output is correct
82 Correct 53 ms 55160 KB Output is correct
83 Correct 52 ms 55288 KB Output is correct
84 Correct 58 ms 55800 KB Output is correct
85 Correct 508 ms 77048 KB Output is correct
86 Correct 58 ms 55804 KB Output is correct
87 Correct 581 ms 76384 KB Output is correct
88 Correct 53 ms 55160 KB Output is correct
89 Correct 559 ms 75680 KB Output is correct
90 Correct 516 ms 76960 KB Output is correct
91 Correct 392 ms 78360 KB Output is correct
92 Correct 496 ms 76160 KB Output is correct
93 Correct 57 ms 55800 KB Output is correct
94 Correct 53 ms 55288 KB Output is correct
95 Correct 392 ms 78448 KB Output is correct
96 Correct 460 ms 81700 KB Output is correct
97 Correct 70 ms 55800 KB Output is correct
98 Correct 55 ms 55388 KB Output is correct
99 Correct 454 ms 77296 KB Output is correct
100 Correct 59 ms 55772 KB Output is correct
101 Correct 541 ms 76408 KB Output is correct
102 Correct 435 ms 80504 KB Output is correct
103 Correct 73 ms 55928 KB Output is correct
104 Correct 58 ms 55912 KB Output is correct
105 Correct 58 ms 55800 KB Output is correct
106 Correct 58 ms 55804 KB Output is correct
107 Correct 58 ms 55772 KB Output is correct
108 Correct 2990 ms 165868 KB Output is correct
109 Execution timed out 3079 ms 188336 KB Time limit exceeded
110 Halted 0 ms 0 KB -