Submission #246626

# Submission time Handle Problem Language Result Execution time Memory
246626 2020-07-09T17:53:58 Z Kastanda Bridges (APIO19_bridges) C++11
100 / 100
2589 ms 9188 KB
// M
#include<bits/stdc++.h>
using namespace std;
const int N = 100005, SQ = 831;
int n, m, q, A[N], B[N], W[N], Rev[N];
int TP[N], V[N], Z[N], ME[N], Rs[N];
int P[N], P2[N], Fw[SQ + 5][SQ + 5];
int Find(int v)
{
        return (P[v] < 0 ? v : (P[v] = Find(P[v])));
}
int Find2(int v)
{
        return (P2[v] < 0 ? v : (P2[v] = Find2(P2[v])));
}
inline void Merge(int v, int u)
{
        v = Find(v);
        u = Find(u);
        if (v != u)
                P[v] += P[u], P[u] = v;
}
inline void Merge2(int v, int u)
{
        v = Find2(v);
        u = Find2(u);
        if (v != u)
                P2[v] += P2[u], P2[u] = v;
}
int main()
{
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= m; i ++)
        {
                scanf("%d%d%d", &A[i], &B[i], &W[i]);
        }
        scanf("%d", &q);
        for (int i = 1; i <= q; i ++)
                scanf("%d%d%d", &TP[i], &V[i], &Z[i]);
        vector < int > E;
        for (int i = 1; i <= m; i ++)
                E.push_back(i);
        sort(E.begin(), E.end(), [&](int i, int j){return W[i] > W[j];});
        for (int l = 1; l <= q; l += SQ)
        {
                int r = min(l + SQ - 1, q);

                // Eliminating marked edges from E .. :
                for (int i = l; i <= r; i ++)
                        if (TP[i] == 1) ME[V[i]] = i;
                vector < int > tmpE, MarkE;
                for (int id : E)
                        if (!ME[id])
                                tmpE.push_back(id);
                        else
                                MarkE.push_back(id), Rev[id] = (int)MarkE.size() - 1;
                E.swap(tmpE);
                tmpE.clear();
                // Done with this thing

                vector < int > Q;
                for (int i = l; i <= r; i ++)
                        if (TP[i] == 2) Q.push_back(i);
                sort(Q.begin(), Q.end(), [&](int i, int j){return Z[i] > Z[j];});

                // Vertices of interset :
                vector < int > Voi;
                for (int i = l; i <= r; i ++)
                        if (TP[i] == 1)
                                Voi.push_back(A[V[i]]),
                                Voi.push_back(B[V[i]]);
                        else
                                Voi.push_back(V[i]);

                // Calculating last instance of each edge for each query ...
                vector < int > prev((int)MarkE.size());
                for (int i = 0; i < MarkE.size(); i ++)
                        prev[i] = W[MarkE[i]];
                for (int i = l; i <= r; i ++)
                {
                        if (TP[i] == 1)
                                prev[Rev[V[i]]] = Z[i];
                        else
                                for (int j = 0; j < (int)MarkE.size(); j ++)
                                        Fw[i - l][j] = prev[j];
                }
                // Phew, all done ...


                int ptre = 0;
                memset(P2, 0, sizeof(P2));
                memset(P, -1, sizeof(P));
                for (int i : Q)
                {
                        while (ptre < (int)E.size() && W[E[ptre]] >= Z[i])
                                Merge(A[E[ptre]], B[E[ptre]]), ptre ++;
                        for (int v : Voi)
                                Find(v), P2[v] = P[v], P2[Find(v)] = P[Find(v)];

                        for (int j = 0; j < (int)MarkE.size(); j ++)
                                if (Fw[i - l][j] >= Z[i])
                                        Merge2(A[MarkE[j]], B[MarkE[j]]);

                        Rs[i] = - P2[Find2(V[i])];
                }

                for (int i = l; i <= r; i ++)
                        if (TP[i] == 1)
                                ME[V[i]] = 0, W[V[i]] = Z[i];

                sort(MarkE.begin(), MarkE.end(), [&](int i, int j){return W[i] > W[j];});

                int ptme = 0;
                tmpE.clear();
                for (int e : E)
                {
                        while (ptme < (int)MarkE.size() && W[MarkE[ptme]] > W[e])
                                tmpE.push_back(MarkE[ptme]), ptme ++;
                        tmpE.push_back(e);
                }
                while (ptme < (int)MarkE.size())
                        tmpE.push_back(MarkE[ptme]), ptme ++;
                E.swap(tmpE);
                tmpE.clear();
                assert((int)E.size() == m);

        }

        for (int i = 1; i <= q; i ++)
                if (TP[i] == 2)
                        printf("%d\n", Rs[i]);
        return 0;
}

Compilation message

bridges.cpp: In function 'int main()':
bridges.cpp:77:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 for (int i = 0; i < MarkE.size(); i ++)
                                 ~~^~~~~~~~~~~~~~
bridges.cpp:32:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &n, &m);
         ~~~~~^~~~~~~~~~~~~~~~
bridges.cpp:35:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
                 scanf("%d%d%d", &A[i], &B[i], &W[i]);
                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:37:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &q);
         ~~~~~^~~~~~~~~~
bridges.cpp:39:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
                 scanf("%d%d%d", &TP[i], &V[i], &Z[i]);
                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1152 KB Output is correct
2 Correct 5 ms 1152 KB Output is correct
3 Correct 77 ms 4216 KB Output is correct
4 Correct 36 ms 1408 KB Output is correct
5 Correct 38 ms 4216 KB Output is correct
6 Correct 36 ms 4216 KB Output is correct
7 Correct 36 ms 4088 KB Output is correct
8 Correct 48 ms 4216 KB Output is correct
9 Correct 49 ms 4088 KB Output is correct
10 Correct 36 ms 4216 KB Output is correct
11 Correct 36 ms 4216 KB Output is correct
12 Correct 45 ms 4224 KB Output is correct
13 Correct 44 ms 4216 KB Output is correct
14 Correct 38 ms 4224 KB Output is correct
15 Correct 74 ms 4216 KB Output is correct
16 Correct 37 ms 4096 KB Output is correct
17 Correct 39 ms 4096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1171 ms 7288 KB Output is correct
2 Correct 1171 ms 7432 KB Output is correct
3 Correct 1177 ms 7376 KB Output is correct
4 Correct 1172 ms 7388 KB Output is correct
5 Correct 1200 ms 7440 KB Output is correct
6 Correct 1709 ms 7480 KB Output is correct
7 Correct 1718 ms 7444 KB Output is correct
8 Correct 1728 ms 7524 KB Output is correct
9 Correct 308 ms 3064 KB Output is correct
10 Correct 1358 ms 7288 KB Output is correct
11 Correct 1286 ms 7332 KB Output is correct
12 Correct 1598 ms 7480 KB Output is correct
13 Correct 695 ms 7224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1037 ms 6768 KB Output is correct
2 Correct 1006 ms 6008 KB Output is correct
3 Correct 1255 ms 6776 KB Output is correct
4 Correct 1031 ms 6716 KB Output is correct
5 Correct 323 ms 3040 KB Output is correct
6 Correct 1188 ms 6728 KB Output is correct
7 Correct 991 ms 6816 KB Output is correct
8 Correct 869 ms 6712 KB Output is correct
9 Correct 1277 ms 6964 KB Output is correct
10 Correct 1054 ms 6792 KB Output is correct
11 Correct 485 ms 6520 KB Output is correct
12 Correct 437 ms 6520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2291 ms 5476 KB Output is correct
2 Correct 301 ms 3064 KB Output is correct
3 Correct 170 ms 4008 KB Output is correct
4 Correct 141 ms 4012 KB Output is correct
5 Correct 2153 ms 5548 KB Output is correct
6 Correct 2267 ms 5536 KB Output is correct
7 Correct 1978 ms 5548 KB Output is correct
8 Correct 1261 ms 4280 KB Output is correct
9 Correct 1240 ms 4444 KB Output is correct
10 Correct 1479 ms 4464 KB Output is correct
11 Correct 1608 ms 5184 KB Output is correct
12 Correct 1603 ms 5180 KB Output is correct
13 Correct 1764 ms 5080 KB Output is correct
14 Correct 2024 ms 5584 KB Output is correct
15 Correct 1994 ms 5652 KB Output is correct
16 Correct 2237 ms 5692 KB Output is correct
17 Correct 2200 ms 5540 KB Output is correct
18 Correct 2129 ms 5676 KB Output is correct
19 Correct 2143 ms 5424 KB Output is correct
20 Correct 1826 ms 5324 KB Output is correct
21 Correct 1846 ms 5360 KB Output is correct
22 Correct 2260 ms 5644 KB Output is correct
23 Correct 1834 ms 5332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1171 ms 7288 KB Output is correct
2 Correct 1171 ms 7432 KB Output is correct
3 Correct 1177 ms 7376 KB Output is correct
4 Correct 1172 ms 7388 KB Output is correct
5 Correct 1200 ms 7440 KB Output is correct
6 Correct 1709 ms 7480 KB Output is correct
7 Correct 1718 ms 7444 KB Output is correct
8 Correct 1728 ms 7524 KB Output is correct
9 Correct 308 ms 3064 KB Output is correct
10 Correct 1358 ms 7288 KB Output is correct
11 Correct 1286 ms 7332 KB Output is correct
12 Correct 1598 ms 7480 KB Output is correct
13 Correct 695 ms 7224 KB Output is correct
14 Correct 1037 ms 6768 KB Output is correct
15 Correct 1006 ms 6008 KB Output is correct
16 Correct 1255 ms 6776 KB Output is correct
17 Correct 1031 ms 6716 KB Output is correct
18 Correct 323 ms 3040 KB Output is correct
19 Correct 1188 ms 6728 KB Output is correct
20 Correct 991 ms 6816 KB Output is correct
21 Correct 869 ms 6712 KB Output is correct
22 Correct 1277 ms 6964 KB Output is correct
23 Correct 1054 ms 6792 KB Output is correct
24 Correct 485 ms 6520 KB Output is correct
25 Correct 437 ms 6520 KB Output is correct
26 Correct 1180 ms 7296 KB Output is correct
27 Correct 1923 ms 7628 KB Output is correct
28 Correct 1611 ms 7540 KB Output is correct
29 Correct 1072 ms 7388 KB Output is correct
30 Correct 1564 ms 7280 KB Output is correct
31 Correct 1594 ms 7424 KB Output is correct
32 Correct 1644 ms 7608 KB Output is correct
33 Correct 1337 ms 7280 KB Output is correct
34 Correct 1381 ms 7532 KB Output is correct
35 Correct 1392 ms 7252 KB Output is correct
36 Correct 984 ms 7308 KB Output is correct
37 Correct 1008 ms 7272 KB Output is correct
38 Correct 996 ms 7492 KB Output is correct
39 Correct 1037 ms 7540 KB Output is correct
40 Correct 1024 ms 7268 KB Output is correct
41 Correct 1016 ms 7468 KB Output is correct
42 Correct 570 ms 7348 KB Output is correct
43 Correct 578 ms 7208 KB Output is correct
44 Correct 554 ms 7272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1152 KB Output is correct
2 Correct 5 ms 1152 KB Output is correct
3 Correct 77 ms 4216 KB Output is correct
4 Correct 36 ms 1408 KB Output is correct
5 Correct 38 ms 4216 KB Output is correct
6 Correct 36 ms 4216 KB Output is correct
7 Correct 36 ms 4088 KB Output is correct
8 Correct 48 ms 4216 KB Output is correct
9 Correct 49 ms 4088 KB Output is correct
10 Correct 36 ms 4216 KB Output is correct
11 Correct 36 ms 4216 KB Output is correct
12 Correct 45 ms 4224 KB Output is correct
13 Correct 44 ms 4216 KB Output is correct
14 Correct 38 ms 4224 KB Output is correct
15 Correct 74 ms 4216 KB Output is correct
16 Correct 37 ms 4096 KB Output is correct
17 Correct 39 ms 4096 KB Output is correct
18 Correct 1171 ms 7288 KB Output is correct
19 Correct 1171 ms 7432 KB Output is correct
20 Correct 1177 ms 7376 KB Output is correct
21 Correct 1172 ms 7388 KB Output is correct
22 Correct 1200 ms 7440 KB Output is correct
23 Correct 1709 ms 7480 KB Output is correct
24 Correct 1718 ms 7444 KB Output is correct
25 Correct 1728 ms 7524 KB Output is correct
26 Correct 308 ms 3064 KB Output is correct
27 Correct 1358 ms 7288 KB Output is correct
28 Correct 1286 ms 7332 KB Output is correct
29 Correct 1598 ms 7480 KB Output is correct
30 Correct 695 ms 7224 KB Output is correct
31 Correct 1037 ms 6768 KB Output is correct
32 Correct 1006 ms 6008 KB Output is correct
33 Correct 1255 ms 6776 KB Output is correct
34 Correct 1031 ms 6716 KB Output is correct
35 Correct 323 ms 3040 KB Output is correct
36 Correct 1188 ms 6728 KB Output is correct
37 Correct 991 ms 6816 KB Output is correct
38 Correct 869 ms 6712 KB Output is correct
39 Correct 1277 ms 6964 KB Output is correct
40 Correct 1054 ms 6792 KB Output is correct
41 Correct 485 ms 6520 KB Output is correct
42 Correct 437 ms 6520 KB Output is correct
43 Correct 2291 ms 5476 KB Output is correct
44 Correct 301 ms 3064 KB Output is correct
45 Correct 170 ms 4008 KB Output is correct
46 Correct 141 ms 4012 KB Output is correct
47 Correct 2153 ms 5548 KB Output is correct
48 Correct 2267 ms 5536 KB Output is correct
49 Correct 1978 ms 5548 KB Output is correct
50 Correct 1261 ms 4280 KB Output is correct
51 Correct 1240 ms 4444 KB Output is correct
52 Correct 1479 ms 4464 KB Output is correct
53 Correct 1608 ms 5184 KB Output is correct
54 Correct 1603 ms 5180 KB Output is correct
55 Correct 1764 ms 5080 KB Output is correct
56 Correct 2024 ms 5584 KB Output is correct
57 Correct 1994 ms 5652 KB Output is correct
58 Correct 2237 ms 5692 KB Output is correct
59 Correct 2200 ms 5540 KB Output is correct
60 Correct 2129 ms 5676 KB Output is correct
61 Correct 2143 ms 5424 KB Output is correct
62 Correct 1826 ms 5324 KB Output is correct
63 Correct 1846 ms 5360 KB Output is correct
64 Correct 2260 ms 5644 KB Output is correct
65 Correct 1834 ms 5332 KB Output is correct
66 Correct 1180 ms 7296 KB Output is correct
67 Correct 1923 ms 7628 KB Output is correct
68 Correct 1611 ms 7540 KB Output is correct
69 Correct 1072 ms 7388 KB Output is correct
70 Correct 1564 ms 7280 KB Output is correct
71 Correct 1594 ms 7424 KB Output is correct
72 Correct 1644 ms 7608 KB Output is correct
73 Correct 1337 ms 7280 KB Output is correct
74 Correct 1381 ms 7532 KB Output is correct
75 Correct 1392 ms 7252 KB Output is correct
76 Correct 984 ms 7308 KB Output is correct
77 Correct 1008 ms 7272 KB Output is correct
78 Correct 996 ms 7492 KB Output is correct
79 Correct 1037 ms 7540 KB Output is correct
80 Correct 1024 ms 7268 KB Output is correct
81 Correct 1016 ms 7468 KB Output is correct
82 Correct 570 ms 7348 KB Output is correct
83 Correct 578 ms 7208 KB Output is correct
84 Correct 554 ms 7272 KB Output is correct
85 Correct 2498 ms 9160 KB Output is correct
86 Correct 177 ms 7500 KB Output is correct
87 Correct 157 ms 7592 KB Output is correct
88 Correct 2206 ms 9156 KB Output is correct
89 Correct 2589 ms 9056 KB Output is correct
90 Correct 2241 ms 8916 KB Output is correct
91 Correct 1322 ms 7420 KB Output is correct
92 Correct 1349 ms 7388 KB Output is correct
93 Correct 1834 ms 7556 KB Output is correct
94 Correct 1670 ms 8184 KB Output is correct
95 Correct 1660 ms 8336 KB Output is correct
96 Correct 1927 ms 8192 KB Output is correct
97 Correct 2201 ms 8616 KB Output is correct
98 Correct 1371 ms 8396 KB Output is correct
99 Correct 2314 ms 8936 KB Output is correct
100 Correct 2307 ms 9188 KB Output is correct
101 Correct 2273 ms 9004 KB Output is correct
102 Correct 2290 ms 8920 KB Output is correct
103 Correct 2001 ms 8184 KB Output is correct
104 Correct 1978 ms 8116 KB Output is correct
105 Correct 1139 ms 8308 KB Output is correct
106 Correct 867 ms 7888 KB Output is correct
107 Correct 1978 ms 8184 KB Output is correct
108 Correct 2455 ms 8392 KB Output is correct
109 Correct 2113 ms 8380 KB Output is correct