## 답안 #255789

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
255789 2020-08-01T20:40:31 Z mehrdad_sohrabi 다리 (APIO19_bridges) C++14
59 / 100
3000 ms 21660 KB
```#include <bits/stdc++.h>
typedef int ll;
typedef long double ld;
#define pb push_back
#define pii pair < int , int >
#define F first
#define S second
//#define endl '\n'
//#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout<<x<<'\n', 0;
using namespace std;
const int N=5e4+2,sq=500;
vector <pair <int,pii > > yy,yal[N*4];
bool vis[N*2];
bool vis2[N*2];
ll par[N],sz[N];
ll getpar(ll v){
if (par[v]==v) return v;
return par[v]=getpar(par[v]);
}
inline void mrg(ll v,ll u){
ll u1=getpar(u),v1=getpar(v);
if (u1==v1) return ;
if (sz[u1]>sz[v1])swap(u1,v1);
par[u1]=v1;
sz[v1]+=sz[u1];
}
ll pp[N],zz[N];
ll gg(ll v){
if (pp[v]==v) return v;
return pp[v]=gg(pp[v]);
}
inline void merg(ll v,ll u){
ll v1=gg(v),u1=gg(u);
if (v1==u1) return ;
if (zz[u1]>zz[v1]) swap(v1,u1);
pp[u1]=v1;
zz[v1]+=zz[u1];
}
ll ans[N*2];
vector <pair <int,pii> > q;
vector <int> com;
map <int,int> mp;

int32_t main(){
sync;
ll n,m;
scanf("%d %d",&n,&m);
for (int i=1;i<=m;i++){
ll u,v,w;
scanf("%d %d %d",&u,&v,&w);
com.pb(w);
yy.pb({w,{u,v}});
}
ll q1;
scanf("%d",&q1);
while(q1--){
ll u,v,w;
scanf("%d %d %d",&u,&v,&w);
com.pb(w);
q.pb({u,{v,w}});
}
sort(com.begin(),com.end());
com.resize(unique(com.begin(),com.end())-com.begin());
for (int i=0;i<com.size();i++) mp[com[i]]=i;
for (int i=0;i<yy.size();i++){
yy[i].F=mp[yy[i].F];
}
for (int i=0;i<q.size();i++){
q[i].S.S=mp[q[i].S.S];
}
for (int i=0;i<q.size();i+=sq){
vector <pii> g;
for (int j=i;j<min(i+sq,(int)q.size());j++){
if (q[j].F==1) vis[q[j].S.F-1]=1;
else{
g.pb({q[j].S.S,j});
}
}
memset(par,0,sizeof par);
memset(sz,0,sizeof sz);
for (int j=1;j<=n;j++){
sz[j]=1;
par[j]=j;
}
for (int i=0;i<com.size();i++) yal[i].clear();
for (int j=0;j<yy.size();j++){
if (vis[j]) continue;
yal[yy[j].F].pb(yy[j]);
}
sort(g.begin(),g.end());
reverse(g.begin(),g.end());
ll cnt=0;
for (int j=com.size()-1;j>-1;j--){
while(cnt<g.size() && g[cnt].F>j){
for (int k=i;k<min(i+sq,(ll)q.size());k++){
if (q[k].F==1){
vis2[q[k].S.F-1]=0;
ll v=yy[q[k].S.F-1].S.F,u=yy[q[k].S.F-1].S.S;
pp[v]=getpar(v);
pp[u]=getpar(u);
zz[pp[v]]=sz[pp[v]];
zz[pp[u]]=sz[pp[u]];
pp[pp[v]]=pp[v];
pp[pp[u]]=pp[u];
}
else{
ll v=q[k].S.F;
pp[v]=getpar(v);
zz[pp[v]]=sz[pp[v]];
pp[pp[v]]=pp[v];
}
}
ll id=g[cnt].S;
for (int k=id;k>=i;k--){
if (q[k].F==2) continue;
ll v=yy[q[k].S.F-1].S.F,u=yy[q[k].S.F-1].S.S;
if (vis2[q[k].S.F-1]) continue;
vis2[q[k].S.F-1]=1;
if (q[k].S.S>=g[cnt].F){
merg(u,v);
}
}
for (int k=id+1;k<min(i+sq,(ll)q.size());k++){
if (q[k].F==2) continue;
ll v=yy[q[k].S.F-1].S.F,u=yy[q[k].S.F-1].S.S;
if (vis2[q[k].S.F-1]) continue;
vis2[q[k].S.F-1]=1;
if (yy[q[k].S.F-1].F>=g[cnt].F){
merg(u,v);
}
}
ans[id]=zz[gg(q[id].S.F)];
cnt++;
}
for (int jj=0;jj<yal[j].size();jj++){
mrg(yal[j][jj].S.F,yal[j][jj].S.S);
}
}
while(cnt<g.size()){
for (int k=i;k<min(i+sq,(ll)q.size());k++){
if (q[k].F==1){
vis2[q[k].S.F-1]=0;
ll v=yy[q[k].S.F-1].S.F,u=yy[q[k].S.F-1].S.S;
pp[v]=getpar(v);
pp[u]=getpar(u);
zz[pp[v]]=sz[pp[v]];
zz[pp[u]]=sz[pp[u]];
pp[pp[u]]=pp[u];
pp[pp[v]]=pp[v];
}
else{
ll v=q[k].S.F;
pp[v]=getpar(v);
zz[pp[v]]=sz[pp[v]];
pp[pp[v]]=pp[v];
}
}
ll id=g[cnt].S;
for (int k=id;k>=i;k--){
if (q[k].F==2) continue;
ll v=yy[q[k].S.F-1].S.F,u=yy[q[k].S.F-1].S.S;
if (vis2[q[k].S.F-1]) continue;
vis2[q[k].S.F-1]=1;
if (q[k].S.S>=g[cnt].F){
merg(u,v);
}
}
for (int k=id+1;k<min(i+sq,(ll)q.size());k++){
if (q[k].F==2) continue;
ll v=yy[q[k].S.F-1].S.F,u=yy[q[k].S.F-1].S.S;
if (vis2[q[k].S.F-1]) continue;
vis2[q[k].S.F-1]=1;
if (yy[q[k].S.F-1].F>=g[cnt].F){
merg(u,v);
}
}
ans[id]=zz[gg(q[id].S.F)];
cnt++;
}
for (int j=i;j<(min(i+sq,(ll)q.size()));j++){
if (q[j].F==1){
yy[q[j].S.F-1].F=q[j].S.S;
vis[q[j].S.F-1]=0;
}

}
}
for (int i=0;i<q.size();i++){
if (q[i].F==2)
printf("%d \n",ans[i]);
}
}

```

### Compilation message

```bridges.cpp: In function 'int32_t main()':
bridges.cpp:67:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0;i<com.size();i++) mp[com[i]]=i;
~^~~~~~~~~~~
bridges.cpp:68:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0;i<yy.size();i++){
~^~~~~~~~~~
bridges.cpp:71:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0;i<q.size();i++){
~^~~~~~~~~
bridges.cpp:74:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0;i<q.size();i+=sq){
~^~~~~~~~~
bridges.cpp:88:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0;i<com.size();i++) yal[i].clear();
~^~~~~~~~~~~
bridges.cpp:89:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j=0;j<yy.size();j++){
~^~~~~~~~~~
bridges.cpp:97:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(cnt<g.size() && g[cnt].F>j){
~~~^~~~~~~~~
bridges.cpp:138:29: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int jj=0;jj<yal[j].size();jj++){
~~^~~~~~~~~~~~~~
bridges.cpp:142:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(cnt<g.size()){
~~~^~~~~~~~~
bridges.cpp:191:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0;i<q.size();i++){
~^~~~~~~~~
bridges.cpp:50:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&n,&m);
~~~~~^~~~~~~~~~~~~~~
bridges.cpp:53:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d",&u,&v,&w);
~~~~~^~~~~~~~~~~~~~~~~~~~~
bridges.cpp:58:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&q1);
~~~~~^~~~~~~~~~
bridges.cpp:61:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d",&u,&v,&w);
~~~~~^~~~~~~~~~~~~~~~~~~~~```

#### Subtask #1 13.0 / 13.0

# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5376 KB Output is correct
2 Correct 4 ms 5376 KB Output is correct
3 Correct 69 ms 6368 KB Output is correct
4 Correct 37 ms 6144 KB Output is correct
5 Correct 60 ms 6136 KB Output is correct
6 Correct 54 ms 5888 KB Output is correct
7 Correct 54 ms 5844 KB Output is correct
8 Correct 60 ms 6108 KB Output is correct
9 Correct 50 ms 5844 KB Output is correct
10 Correct 59 ms 6108 KB Output is correct
11 Correct 59 ms 6108 KB Output is correct
12 Correct 63 ms 6228 KB Output is correct
13 Correct 70 ms 6112 KB Output is correct
14 Correct 63 ms 6232 KB Output is correct
15 Correct 66 ms 6232 KB Output is correct
16 Correct 51 ms 5848 KB Output is correct
17 Correct 53 ms 5876 KB Output is correct

#### Subtask #2 16.0 / 16.0

# 결과 실행 시간 메모리 Grader output
1 Correct 2241 ms 19096 KB Output is correct
2 Correct 2359 ms 19080 KB Output is correct
3 Correct 2710 ms 18980 KB Output is correct
4 Correct 2253 ms 19188 KB Output is correct
5 Correct 2366 ms 19136 KB Output is correct
6 Correct 2435 ms 18184 KB Output is correct
7 Correct 2293 ms 18800 KB Output is correct
8 Correct 2505 ms 18848 KB Output is correct
9 Correct 414 ms 12452 KB Output is correct
10 Correct 1215 ms 9892 KB Output is correct
11 Correct 1185 ms 10020 KB Output is correct
12 Correct 2011 ms 17316 KB Output is correct
13 Correct 1835 ms 20128 KB Output is correct

#### Subtask #3 17.0 / 17.0

# 결과 실행 시간 메모리 Grader output
1 Correct 1856 ms 17188 KB Output is correct
2 Correct 1055 ms 13440 KB Output is correct
3 Correct 1762 ms 16164 KB Output is correct
4 Correct 1734 ms 17300 KB Output is correct
5 Correct 432 ms 12456 KB Output is correct
6 Correct 1956 ms 17316 KB Output is correct
7 Correct 1923 ms 17188 KB Output is correct
8 Correct 1897 ms 17188 KB Output is correct
9 Correct 1603 ms 16232 KB Output is correct
10 Correct 1382 ms 16292 KB Output is correct
11 Correct 1432 ms 18340 KB Output is correct
12 Correct 1175 ms 18344 KB Output is correct

#### Subtask #4 0 / 14.0

# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3101 ms 21660 KB Time limit exceeded
2 Halted 0 ms 0 KB -

#### Subtask #5 13.0 / 13.0

# 결과 실행 시간 메모리 Grader output
1 Correct 2241 ms 19096 KB Output is correct
2 Correct 2359 ms 19080 KB Output is correct
3 Correct 2710 ms 18980 KB Output is correct
4 Correct 2253 ms 19188 KB Output is correct
5 Correct 2366 ms 19136 KB Output is correct
6 Correct 2435 ms 18184 KB Output is correct
7 Correct 2293 ms 18800 KB Output is correct
8 Correct 2505 ms 18848 KB Output is correct
9 Correct 414 ms 12452 KB Output is correct
10 Correct 1215 ms 9892 KB Output is correct
11 Correct 1185 ms 10020 KB Output is correct
12 Correct 2011 ms 17316 KB Output is correct
13 Correct 1835 ms 20128 KB Output is correct
14 Correct 1856 ms 17188 KB Output is correct
15 Correct 1055 ms 13440 KB Output is correct
16 Correct 1762 ms 16164 KB Output is correct
17 Correct 1734 ms 17300 KB Output is correct
18 Correct 432 ms 12456 KB Output is correct
19 Correct 1956 ms 17316 KB Output is correct
20 Correct 1923 ms 17188 KB Output is correct
21 Correct 1897 ms 17188 KB Output is correct
22 Correct 1603 ms 16232 KB Output is correct
23 Correct 1382 ms 16292 KB Output is correct
24 Correct 1432 ms 18340 KB Output is correct
25 Correct 1175 ms 18344 KB Output is correct
26 Correct 2547 ms 19140 KB Output is correct
27 Correct 2638 ms 17956 KB Output is correct
28 Correct 2513 ms 19236 KB Output is correct
29 Correct 2236 ms 19004 KB Output is correct
30 Correct 2471 ms 18720 KB Output is correct
31 Correct 2524 ms 18592 KB Output is correct
32 Correct 2513 ms 18464 KB Output is correct
33 Correct 2391 ms 19204 KB Output is correct
34 Correct 2583 ms 19188 KB Output is correct
35 Correct 2372 ms 19108 KB Output is correct
36 Correct 2139 ms 19320 KB Output is correct
37 Correct 2387 ms 18972 KB Output is correct
38 Correct 2465 ms 18976 KB Output is correct
39 Correct 1916 ms 17828 KB Output is correct
40 Correct 1973 ms 17944 KB Output is correct
41 Correct 2005 ms 17824 KB Output is correct
42 Correct 1880 ms 20004 KB Output is correct
43 Correct 1908 ms 20000 KB Output is correct
44 Correct 1644 ms 20168 KB Output is correct

#### Subtask #6 0 / 27.0

# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5376 KB Output is correct
2 Correct 4 ms 5376 KB Output is correct
3 Correct 69 ms 6368 KB Output is correct
4 Correct 37 ms 6144 KB Output is correct
5 Correct 60 ms 6136 KB Output is correct
6 Correct 54 ms 5888 KB Output is correct
7 Correct 54 ms 5844 KB Output is correct
8 Correct 60 ms 6108 KB Output is correct
9 Correct 50 ms 5844 KB Output is correct
10 Correct 59 ms 6108 KB Output is correct
11 Correct 59 ms 6108 KB Output is correct
12 Correct 63 ms 6228 KB Output is correct
13 Correct 70 ms 6112 KB Output is correct
14 Correct 63 ms 6232 KB Output is correct
15 Correct 66 ms 6232 KB Output is correct
16 Correct 51 ms 5848 KB Output is correct
17 Correct 53 ms 5876 KB Output is correct
18 Correct 2241 ms 19096 KB Output is correct
19 Correct 2359 ms 19080 KB Output is correct
20 Correct 2710 ms 18980 KB Output is correct
21 Correct 2253 ms 19188 KB Output is correct
22 Correct 2366 ms 19136 KB Output is correct
23 Correct 2435 ms 18184 KB Output is correct
24 Correct 2293 ms 18800 KB Output is correct
25 Correct 2505 ms 18848 KB Output is correct
26 Correct 414 ms 12452 KB Output is correct
27 Correct 1215 ms 9892 KB Output is correct
28 Correct 1185 ms 10020 KB Output is correct
29 Correct 2011 ms 17316 KB Output is correct
30 Correct 1835 ms 20128 KB Output is correct
31 Correct 1856 ms 17188 KB Output is correct
32 Correct 1055 ms 13440 KB Output is correct
33 Correct 1762 ms 16164 KB Output is correct
34 Correct 1734 ms 17300 KB Output is correct
35 Correct 432 ms 12456 KB Output is correct
36 Correct 1956 ms 17316 KB Output is correct
37 Correct 1923 ms 17188 KB Output is correct
38 Correct 1897 ms 17188 KB Output is correct
39 Correct 1603 ms 16232 KB Output is correct
40 Correct 1382 ms 16292 KB Output is correct
41 Correct 1432 ms 18340 KB Output is correct
42 Correct 1175 ms 18344 KB Output is correct
43 Execution timed out 3101 ms 21660 KB Time limit exceeded
44 Halted 0 ms 0 KB -