#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int MAXN = 100010;
int n, m, q;
int raizQ;
int ans[MAXN];
int indInside[MAXN];
int lastWeight[MAXN];
int anc[MAXN], sz[MAXN];
int U[MAXN], V[MAXN], W[MAXN];
bool mark[MAXN];
bool insideBlock[MAXN];
vector<int> adj[MAXN];
vector<pii> queries;
vector< pair<int,pii> > sweep;
int find(int cur)
{
if( anc[cur] == cur ) return cur;
return anc[cur] = find( anc[cur] );
}
void join(int i, int j)
{
i = find(i); j = find(j);
if( i == j ) return;
if( sz[i] < sz[j] )
swap( i , j );
anc[j] = i;
sz[i] += sz[j];
}
bool isUpdate(int i)
{
int val = queries[i].second;
return ( val > 0 );
}
void initDSU()
{
for(int i = 1 ; i <= n ; i++)
sz[i] = 1, anc[i] = i;
}
int DFS(int cur)
{
mark[cur] = true;
int ans = sz[cur];
for(int i = 0 ; i < (int)adj[cur].size() ; i++)
if( !mark[ adj[cur][i] ] ) ans += DFS( adj[cur][i] );
return ans;
}
int main()
{
scanf("%d %d",&n,&m);
for(int i = 1 ; i <= m ; i++)
scanf("%d %d %d",&U[i],&V[i],&W[i]);
scanf("%d",&q);
raizQ = sqrt(q) + 1;
for(int i = 1 ; i <= q ; i++)
{
int type;
scanf("%d",&type);
if( type == 1 )
{
int edge, newW;
scanf("%d %d",&edge,&newW);
queries.push_back( { newW , edge } );
}
if( type == 2 )
{
int s, maxW;
scanf("%d %d",&s,&maxW);
queries.push_back( { maxW , -s } );
}
ans[i - 1] = -1;
}
int lastBlock = (q - 1)/raizQ;
for(int block = 0 ; block <= lastBlock ; block++)
{
//printf("+============================+ Processando bloco %d\n",block);
int L = block*raizQ;
int R = (block + 1)*raizQ - 1;
R = min( R , q - 1 );
for(int i = 1 ; i <= m ; i++)
{
lastWeight[i] = W[i];
insideBlock[i] = false;
}
vector<int> inside;
for(int i = L ; i <= R ; i++)
{
if( isUpdate( i ) )
{
int edge = queries[i].second;
if( !insideBlock[edge] )
{
indInside[edge] = (int)inside.size();
//printf(".. edge = %d\n",edge);
insideBlock[edge] = true;
inside.push_back( edge );
}
}
}
for(int i = 0 ; i < L ; i++)
{
if( isUpdate( i ) )
{
int newW = queries[i].first;
int edge = queries[i].second;
lastWeight[edge] = newW;
}
}
sweep.clear();
for(int i = 1 ; i <= m ; i++)
if( !insideBlock[i] ) sweep.push_back( { lastWeight[i] , { U[i] , V[i] } } );//, printf("Adicionei %d: %d -> %d com peso %d\n",i,U[i],V[i],lastWeight[i]);
for(int i = L ; i <= R ; i++)
{
if( !isUpdate( i ) )
{
int maxW = queries[i].first;
int source = -queries[i].second;
//printf("Achei query %d %d\n",source,maxW);
sweep.push_back( { maxW , { -source , i } } );
}
}
//printf("\n\n\n");
//printf("------- Sweep Algorithm\n");
sort( sweep.begin() , sweep.end() );
initDSU();
for(int i = (int)sweep.size() - 1 ; i >= 0 ; i--)
{
int type = 0;
if( sweep[i].second.first < 0 ) type = 1;
if( type == 0 )//Update
{
int curU = sweep[i].second.first;
int curV = sweep[i].second.second;
//printf("Juntei %d %d\n",curU,curV);
join( curU , curV );
}
if( type == 1 )
{
int maxW = sweep[i].first;
int ind = sweep[i].second.second;
int source = -sweep[i].second.first;
//printf("Respondendo query maxW = %d source = %d ind = %d\n",maxW,source,ind);
vector<int> weightInside;
for(int j = 0 ; j < (int)inside.size() ; j++)
weightInside.push_back( lastWeight[ inside[j] ] );
for(int j = L ; j <= ind ; j++)
{
if( isUpdate( j ) )
{
int newW = queries[j].first;
int edge = queries[j].second;
weightInside[ indInside[edge] ] = newW;
}
}
for(int j = 0 ; j < (int)inside.size() ; j++)
{
//printf("...... Tentando colocar %d -> %d com peso %d\n",U[ inside[j] ],V[ inside[j] ],weightInside[j]);
if( weightInside[j] >= maxW )
{
int curU = find( U[ inside[j] ] );
int curV = find( V[ inside[j] ] );
adj[curU].push_back( curV );
adj[curV].push_back( curU );
}
}
ans[ind] = DFS( find(source) );
mark[ find(source) ] = false;
for(int j = 0 ; j < (int)inside.size() ; j++)
{
if( weightInside[j] >= maxW )
{
int curU = find( U[ inside[j] ] );
int curV = find( V[ inside[j] ] );
mark[curU] = mark[curV] = false;
adj[curU].clear();
adj[curV].clear();
}
}
}
}
}
for(int i = 0 ; i < q ; i++)
if( ans[i] != -1 ) printf("%d\n",ans[i]);
}
Compilation message
bridges.cpp: In function 'int main()':
bridges.cpp:69:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&n,&m);
~~~~~^~~~~~~~~~~~~~~
bridges.cpp:72:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d",&U[i],&V[i],&W[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:74:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&q);
~~~~~^~~~~~~~~
bridges.cpp:80:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&type);
~~~~~^~~~~~~~~~~~
bridges.cpp:85:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&edge,&newW);
~~~~~^~~~~~~~~~~~~~~~~~~~~
bridges.cpp:92:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&s,&maxW);
~~~~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
27 ms |
2944 KB |
Output is correct |
4 |
Correct |
8 ms |
2944 KB |
Output is correct |
5 |
Correct |
18 ms |
2944 KB |
Output is correct |
6 |
Correct |
17 ms |
2944 KB |
Output is correct |
7 |
Correct |
19 ms |
2944 KB |
Output is correct |
8 |
Correct |
19 ms |
2944 KB |
Output is correct |
9 |
Correct |
21 ms |
2944 KB |
Output is correct |
10 |
Correct |
19 ms |
2944 KB |
Output is correct |
11 |
Correct |
25 ms |
2944 KB |
Output is correct |
12 |
Correct |
20 ms |
2944 KB |
Output is correct |
13 |
Correct |
21 ms |
2944 KB |
Output is correct |
14 |
Correct |
20 ms |
2944 KB |
Output is correct |
15 |
Correct |
20 ms |
2944 KB |
Output is correct |
16 |
Correct |
19 ms |
2944 KB |
Output is correct |
17 |
Correct |
20 ms |
2944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2463 ms |
7500 KB |
Output is correct |
2 |
Correct |
2436 ms |
7404 KB |
Output is correct |
3 |
Correct |
2447 ms |
7508 KB |
Output is correct |
4 |
Correct |
2430 ms |
7404 KB |
Output is correct |
5 |
Correct |
2415 ms |
7404 KB |
Output is correct |
6 |
Correct |
2764 ms |
7020 KB |
Output is correct |
7 |
Correct |
2742 ms |
8004 KB |
Output is correct |
8 |
Correct |
2709 ms |
7892 KB |
Output is correct |
9 |
Correct |
81 ms |
5100 KB |
Output is correct |
10 |
Correct |
2355 ms |
8488 KB |
Output is correct |
11 |
Correct |
2461 ms |
8464 KB |
Output is correct |
12 |
Correct |
2156 ms |
8044 KB |
Output is correct |
13 |
Correct |
2203 ms |
7508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1825 ms |
6568 KB |
Output is correct |
2 |
Correct |
972 ms |
4716 KB |
Output is correct |
3 |
Correct |
1958 ms |
6380 KB |
Output is correct |
4 |
Correct |
1799 ms |
6380 KB |
Output is correct |
5 |
Correct |
89 ms |
4200 KB |
Output is correct |
6 |
Correct |
1864 ms |
6504 KB |
Output is correct |
7 |
Correct |
1751 ms |
6308 KB |
Output is correct |
8 |
Correct |
1644 ms |
6436 KB |
Output is correct |
9 |
Correct |
1447 ms |
6304 KB |
Output is correct |
10 |
Correct |
1401 ms |
6248 KB |
Output is correct |
11 |
Correct |
1486 ms |
6288 KB |
Output is correct |
12 |
Correct |
1463 ms |
6376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3078 ms |
7916 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2463 ms |
7500 KB |
Output is correct |
2 |
Correct |
2436 ms |
7404 KB |
Output is correct |
3 |
Correct |
2447 ms |
7508 KB |
Output is correct |
4 |
Correct |
2430 ms |
7404 KB |
Output is correct |
5 |
Correct |
2415 ms |
7404 KB |
Output is correct |
6 |
Correct |
2764 ms |
7020 KB |
Output is correct |
7 |
Correct |
2742 ms |
8004 KB |
Output is correct |
8 |
Correct |
2709 ms |
7892 KB |
Output is correct |
9 |
Correct |
81 ms |
5100 KB |
Output is correct |
10 |
Correct |
2355 ms |
8488 KB |
Output is correct |
11 |
Correct |
2461 ms |
8464 KB |
Output is correct |
12 |
Correct |
2156 ms |
8044 KB |
Output is correct |
13 |
Correct |
2203 ms |
7508 KB |
Output is correct |
14 |
Correct |
1825 ms |
6568 KB |
Output is correct |
15 |
Correct |
972 ms |
4716 KB |
Output is correct |
16 |
Correct |
1958 ms |
6380 KB |
Output is correct |
17 |
Correct |
1799 ms |
6380 KB |
Output is correct |
18 |
Correct |
89 ms |
4200 KB |
Output is correct |
19 |
Correct |
1864 ms |
6504 KB |
Output is correct |
20 |
Correct |
1751 ms |
6308 KB |
Output is correct |
21 |
Correct |
1644 ms |
6436 KB |
Output is correct |
22 |
Correct |
1447 ms |
6304 KB |
Output is correct |
23 |
Correct |
1401 ms |
6248 KB |
Output is correct |
24 |
Correct |
1486 ms |
6288 KB |
Output is correct |
25 |
Correct |
1463 ms |
6376 KB |
Output is correct |
26 |
Correct |
2440 ms |
8368 KB |
Output is correct |
27 |
Correct |
2749 ms |
8428 KB |
Output is correct |
28 |
Correct |
2558 ms |
8456 KB |
Output is correct |
29 |
Correct |
2316 ms |
8476 KB |
Output is correct |
30 |
Correct |
2714 ms |
8604 KB |
Output is correct |
31 |
Correct |
2770 ms |
8596 KB |
Output is correct |
32 |
Correct |
2794 ms |
8580 KB |
Output is correct |
33 |
Correct |
2554 ms |
10248 KB |
Output is correct |
34 |
Correct |
2558 ms |
10340 KB |
Output is correct |
35 |
Correct |
2561 ms |
10092 KB |
Output is correct |
36 |
Correct |
2392 ms |
10040 KB |
Output is correct |
37 |
Correct |
2368 ms |
10028 KB |
Output is correct |
38 |
Correct |
2400 ms |
10264 KB |
Output is correct |
39 |
Correct |
2169 ms |
9632 KB |
Output is correct |
40 |
Correct |
2125 ms |
9624 KB |
Output is correct |
41 |
Correct |
2125 ms |
9628 KB |
Output is correct |
42 |
Correct |
2139 ms |
10132 KB |
Output is correct |
43 |
Correct |
2187 ms |
10068 KB |
Output is correct |
44 |
Correct |
2182 ms |
10216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
27 ms |
2944 KB |
Output is correct |
4 |
Correct |
8 ms |
2944 KB |
Output is correct |
5 |
Correct |
18 ms |
2944 KB |
Output is correct |
6 |
Correct |
17 ms |
2944 KB |
Output is correct |
7 |
Correct |
19 ms |
2944 KB |
Output is correct |
8 |
Correct |
19 ms |
2944 KB |
Output is correct |
9 |
Correct |
21 ms |
2944 KB |
Output is correct |
10 |
Correct |
19 ms |
2944 KB |
Output is correct |
11 |
Correct |
25 ms |
2944 KB |
Output is correct |
12 |
Correct |
20 ms |
2944 KB |
Output is correct |
13 |
Correct |
21 ms |
2944 KB |
Output is correct |
14 |
Correct |
20 ms |
2944 KB |
Output is correct |
15 |
Correct |
20 ms |
2944 KB |
Output is correct |
16 |
Correct |
19 ms |
2944 KB |
Output is correct |
17 |
Correct |
20 ms |
2944 KB |
Output is correct |
18 |
Correct |
2463 ms |
7500 KB |
Output is correct |
19 |
Correct |
2436 ms |
7404 KB |
Output is correct |
20 |
Correct |
2447 ms |
7508 KB |
Output is correct |
21 |
Correct |
2430 ms |
7404 KB |
Output is correct |
22 |
Correct |
2415 ms |
7404 KB |
Output is correct |
23 |
Correct |
2764 ms |
7020 KB |
Output is correct |
24 |
Correct |
2742 ms |
8004 KB |
Output is correct |
25 |
Correct |
2709 ms |
7892 KB |
Output is correct |
26 |
Correct |
81 ms |
5100 KB |
Output is correct |
27 |
Correct |
2355 ms |
8488 KB |
Output is correct |
28 |
Correct |
2461 ms |
8464 KB |
Output is correct |
29 |
Correct |
2156 ms |
8044 KB |
Output is correct |
30 |
Correct |
2203 ms |
7508 KB |
Output is correct |
31 |
Correct |
1825 ms |
6568 KB |
Output is correct |
32 |
Correct |
972 ms |
4716 KB |
Output is correct |
33 |
Correct |
1958 ms |
6380 KB |
Output is correct |
34 |
Correct |
1799 ms |
6380 KB |
Output is correct |
35 |
Correct |
89 ms |
4200 KB |
Output is correct |
36 |
Correct |
1864 ms |
6504 KB |
Output is correct |
37 |
Correct |
1751 ms |
6308 KB |
Output is correct |
38 |
Correct |
1644 ms |
6436 KB |
Output is correct |
39 |
Correct |
1447 ms |
6304 KB |
Output is correct |
40 |
Correct |
1401 ms |
6248 KB |
Output is correct |
41 |
Correct |
1486 ms |
6288 KB |
Output is correct |
42 |
Correct |
1463 ms |
6376 KB |
Output is correct |
43 |
Execution timed out |
3078 ms |
7916 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |