Submission #123655

#TimeUsernameProblemLanguageResultExecution timeMemory
123655dualityBridges (APIO19_bridges)C++11
100 / 100
2306 ms11816 KiB
#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 ---------- int u[100000],v[100000],d[100000]; int t[100000],x[100000],w[100000]; int order[100000],temp1[100000],temp2[100000]; bool comp(int a,int b) { return d[a] > d[b]; } int change[100000],seen[100000]; int ans[100000]; int parent[50000],size[50000]; int find(int n) { if (parent[n] != n) parent[n] = find(parent[n]); return parent[n]; } int merge(int a,int b) { int pa = find(a),pb = find(b); if (pa != pb) { if (size[pa] < size[pb]) swap(pa,pb); parent[pb] = pa,size[pa] += size[pb]; } return 0; } vi adjList[50000]; int visited[50000]; int doDFS(int u) { if (visited[u]) return 0; int i; int ans = size[u]; visited[u] = 1; for (i = 0; i < adjList[u].size(); i++) ans += doDFS(adjList[u][i]); return ans; } int main() { int i; int n,m,q; scanf("%d %d",&n,&m); for (i = 0; i < m; i++) scanf("%d %d %d",&u[i],&v[i],&d[i]),u[i]--,v[i]--; scanf("%d",&q); for (i = 0; i < q; i++) scanf("%d %d %d",&t[i],&x[i],&w[i]),x[i]--; int j,k,l; int bs = 1.5*sqrt(q)+EPS; for (i = 0; i < m; i++) order[i] = i; sort(order,order+m,comp); for (i = 0; i < q; i += bs) { int e = min(i+bs,q); vector<pair<pii,int> > queries; for (j = i; j < e; j++) { if (t[j] == 1) change[x[j]] = 1; else queries.pb(mp(mp(w[j],x[j]),j)); } sort(queries.begin(),queries.end()); for (j = 0; j < n; j++) parent[j] = j,size[j] = 1; k = 0; for (j = queries.size()-1; j >= 0; j--) { while ((k < m) && (d[order[k]] >= queries[j].first.first)) { if (!change[order[k]]) merge(u[order[k]],v[order[k]]); k++; } vi toClear; for (l = queries[j].second; l >= i; l--) { if ((t[l] == 1) && !seen[x[l]]) { seen[x[l]] = 1; if (w[l] >= queries[j].first.first) { int pu = find(u[x[l]]),pv = find(v[x[l]]); adjList[pu].pb(pv),adjList[pv].pb(pu); toClear.pb(pu),toClear.pb(pv); } } } for (l = queries[j].second+1; l < e; l++) { if ((t[l] == 1) && !seen[x[l]] && (d[x[l]] >= queries[j].first.first)) { int pu = find(u[x[l]]),pv = find(v[x[l]]); adjList[pu].pb(pv),adjList[pv].pb(pu); toClear.pb(pu),toClear.pb(pv); } } ans[queries[j].second] = doDFS(find(queries[j].first.second)); toClear.pb(find(queries[j].first.second)); for (l = 0; l < toClear.size(); l++) adjList[toClear[l]].clear(),visited[toClear[l]] = 0; for (l = queries[j].second; l >= i; l--) { if (t[l] == 1) seen[x[l]] = 0; } } for (j = i; j < e; j++) { if (t[j] == 1) d[x[j]] = w[j]; } int a = 0,b = 0; for (j = 0; j < m; j++) { if (!change[order[j]]) temp1[a++] = order[j]; } for (j = i; j < e; j++) { if ((t[j] == 1) && change[x[j]]) change[x[j]] = 0,temp2[b++] = x[j]; } sort(temp2,temp2+b,comp); merge(temp1,temp1+a,temp2,temp2+b,order,comp); } for (i = 0; i < q; i++) { if (t[i] == 2) printf("%d\n",ans[i]); } return 0; }

Compilation message (stderr)

bridges.cpp: In function 'int doDFS(int)':
bridges.cpp:86:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i = 0; i < adjList[u].size(); i++) ans += doDFS(adjList[u][i]);
                 ~~^~~~~~~~~~~~~~~~~~~
bridges.cpp: In function 'int main()':
bridges.cpp:136: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;
                         ~~^~~~~~~~~~~~~~~~
bridges.cpp:92: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:93:71: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for (i = 0; i < m; i++) scanf("%d %d %d",&u[i],&v[i],&d[i]),u[i]--,v[i]--;
                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
bridges.cpp:94:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&q);
     ~~~~~^~~~~~~~~
bridges.cpp:95:64: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for (i = 0; i < q; i++) scanf("%d %d %d",&t[i],&x[i],&w[i]),x[i]--;
                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...