#define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
#include "collapse.h"
map<pii,int> M;
int u[100000],v[100000],pos[100000];
vpii queries[100000];
int active[100000],change[100000],seen[100000];
vpii edges;
bool comp(pii a,pii b) {
return a.second < b.second;
}
int ans[100000];
int parent[100000];
int find(int n) {
if (parent[n] != n) parent[n] = find(parent[n]);
return parent[n];
}
vi adjList[100000];
int visited[100000];
int doDFS(int u) {
int i;
int c = 0;
visited[u] = 1;
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if (!visited[v]) c += 1+doDFS(v);
}
return c;
}
vector<int> simulateCollapse(int N,vector<int> T,vector<int> X,vector<int> Y,vector<int> W,vector<int> P) {
int i,j,k,l;
int C = T.size(),Q = W.size(),E = 0;
for (i = 0; i < C; i++) {
if (X[i] > Y[i]) swap(X[i],Y[i]);
if (!M.count(mp(X[i],Y[i]))) u[E] = X[i],v[E] = Y[i],M[mp(X[i],Y[i])] = E++;
pos[i] = M[mp(X[i],Y[i])];
}
for (i = 0; i < Q; i++) queries[W[i]].pb(mp(P[i],i));
int bs = C;//sqrt(C)+EPS;
for (i = 0; i < C; i += bs) {
int e = min(i+bs,C);
vector<pair<pii,int> > qq;
for (j = i; j < e; j++) {
change[pos[j]] = 1;
for (k = 0; k < queries[j].size(); k++) qq.pb(mp(mp(queries[j][k].first,j),queries[j][k].second));
}
for (j = 0; j < E; j++) {
if (active[j] && !change[j]) edges.pb(mp(u[j],v[j]));
}
sort(edges.begin(),edges.end(),greater<pii>());
sort(qq.begin(),qq.end());
for (j = 0; j < N; j++) parent[j] = j;
k = 0;
int c = 0;
for (j = (int) qq.size()-1; j >= 0; j--) {
while ((k < edges.size()) && (edges[k].first > qq[j].first.first)) {
if (find(edges[k].first) != find(edges[k].second)) {
parent[find(edges[k].first)] = find(edges[k].second);
c++;
}
k++;
}
vi toClear;
for (l = qq[j].first.second; l >= i; l--) {
if (!seen[pos[l]]) {
seen[pos[l]] = 1;
if ((T[l] == 0) && (X[l] > qq[j].first.first)) {
int px = find(X[l]),py = find(Y[l]);
adjList[px].pb(py),adjList[py].pb(px);
toClear.pb(px),toClear.pb(py);
}
}
}
for (l = qq[j].first.second+1; l < e; l++) {
if (!seen[pos[l]] && active[pos[l]] && (X[l] > qq[j].first.first)) {
int px = find(X[l]),py = find(Y[l]);
adjList[px].pb(py),adjList[py].pb(px);
toClear.pb(px),toClear.pb(py);
}
}
ans[qq[j].second] += N-qq[j].first.first-1-c;
for (l = 0; l < toClear.size(); l++) {
if (!visited[toClear[l]]) ans[qq[j].second] -= doDFS(toClear[l]);
}
for (l = 0; l < toClear.size(); l++) adjList[toClear[l]].clear(),visited[toClear[l]] = 0;
for (l = qq[j].first.second; l >= i; l--) seen[pos[l]] = 0;
}
sort(edges.begin(),edges.end(),comp);
for (j = 0; j < N; j++) parent[j] = j;
k = c = 0;
for (j = 0; j < qq.size(); j++) {
while ((k < edges.size()) && (edges[k].second <= qq[j].first.first)) {
if (find(edges[k].first) != find(edges[k].second)) {
parent[find(edges[k].first)] = find(edges[k].second);
c++;
}
k++;
}
vi toClear;
for (l = qq[j].first.second; l >= i; l--) {
if (!seen[pos[l]]) {
seen[pos[l]] = 1;
if ((T[l] == 0) && (Y[l] <= qq[j].first.first)) {
int px = find(X[l]),py = find(Y[l]);
adjList[px].pb(py),adjList[py].pb(px);
toClear.pb(px),toClear.pb(py);
}
}
}
for (l = qq[j].first.second+1; l < e; l++) {
if (!seen[pos[l]] && active[pos[l]] && (Y[l] <= qq[j].first.first)) {
int px = find(X[l]),py = find(Y[l]);
adjList[px].pb(py),adjList[py].pb(px);
toClear.pb(px),toClear.pb(py);
}
}
ans[qq[j].second] += qq[j].first.first+1-c;
for (l = 0; l < toClear.size(); l++) {
if (!visited[toClear[l]]) ans[qq[j].second] -= doDFS(toClear[l]);
}
for (l = 0; l < toClear.size(); l++) adjList[toClear[l]].clear(),visited[toClear[l]] = 0;
for (l = qq[j].first.second; l >= i; l--) seen[pos[l]] = 0;
}
for (j = i; j < e; j++) active[pos[j]] ^= 1,change[pos[j]] = 0;
edges.clear();
}
vi ret;
for (i = 0; i < Q; i++) ret.pb(ans[i]);
return ret;
}
Compilation message
collapse.cpp: In function 'int doDFS(int)':
collapse.cpp:79:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
collapse.cpp: In function 'std::vector<int> simulateCollapse(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
collapse.cpp:100:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (k = 0; k < queries[j].size(); k++) qq.pb(mp(mp(queries[j][k].first,j),queries[j][k].second));
~~^~~~~~~~~~~~~~~~~~~
collapse.cpp:111:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while ((k < edges.size()) && (edges[k].first > qq[j].first.first)) {
~~^~~~~~~~~~~~~~
collapse.cpp:137:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (l = 0; l < toClear.size(); l++) {
~~^~~~~~~~~~~~~~~~
collapse.cpp:140:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (l = 0; l < toClear.size(); l++) adjList[toClear[l]].clear(),visited[toClear[l]] = 0;
~~^~~~~~~~~~~~~~~~
collapse.cpp:146:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (j = 0; j < qq.size(); j++) {
~~^~~~~~~~~~~
collapse.cpp:147:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while ((k < edges.size()) && (edges[k].second <= qq[j].first.first)) {
~~^~~~~~~~~~~~~~
collapse.cpp:173:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (l = 0; l < toClear.size(); l++) {
~~^~~~~~~~~~~~~~~~
collapse.cpp:176:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (l = 0; l < toClear.size(); l++) adjList[toClear[l]].clear(),visited[toClear[l]] = 0;
~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
126 ms |
5644 KB |
Output is correct |
2 |
Correct |
9 ms |
5368 KB |
Output is correct |
3 |
Correct |
12 ms |
5340 KB |
Output is correct |
4 |
Correct |
15 ms |
5368 KB |
Output is correct |
5 |
Correct |
143 ms |
5624 KB |
Output is correct |
6 |
Correct |
555 ms |
6264 KB |
Output is correct |
7 |
Correct |
9 ms |
5492 KB |
Output is correct |
8 |
Correct |
11 ms |
5496 KB |
Output is correct |
9 |
Correct |
290 ms |
6008 KB |
Output is correct |
10 |
Correct |
484 ms |
6072 KB |
Output is correct |
11 |
Correct |
972 ms |
6376 KB |
Output is correct |
12 |
Correct |
793 ms |
6356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
9708 KB |
Output is correct |
2 |
Correct |
56 ms |
9904 KB |
Output is correct |
3 |
Execution timed out |
15094 ms |
15600 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
9708 KB |
Output is correct |
2 |
Correct |
76 ms |
9704 KB |
Output is correct |
3 |
Correct |
119 ms |
10016 KB |
Output is correct |
4 |
Correct |
183 ms |
10120 KB |
Output is correct |
5 |
Correct |
1247 ms |
10124 KB |
Output is correct |
6 |
Correct |
10846 ms |
11080 KB |
Output is correct |
7 |
Execution timed out |
15045 ms |
20280 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
126 ms |
5644 KB |
Output is correct |
2 |
Correct |
9 ms |
5368 KB |
Output is correct |
3 |
Correct |
12 ms |
5340 KB |
Output is correct |
4 |
Correct |
15 ms |
5368 KB |
Output is correct |
5 |
Correct |
143 ms |
5624 KB |
Output is correct |
6 |
Correct |
555 ms |
6264 KB |
Output is correct |
7 |
Correct |
9 ms |
5492 KB |
Output is correct |
8 |
Correct |
11 ms |
5496 KB |
Output is correct |
9 |
Correct |
290 ms |
6008 KB |
Output is correct |
10 |
Correct |
484 ms |
6072 KB |
Output is correct |
11 |
Correct |
972 ms |
6376 KB |
Output is correct |
12 |
Correct |
793 ms |
6356 KB |
Output is correct |
13 |
Correct |
44 ms |
9708 KB |
Output is correct |
14 |
Correct |
56 ms |
9904 KB |
Output is correct |
15 |
Execution timed out |
15094 ms |
15600 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |