Submission #246621

# Submission time Handle Problem Language Result Execution time Memory
246621 2020-07-09T17:46:39 Z Kastanda Bridges (APIO19_bridges) C++11
59 / 100
3000 ms 9092 KB
// M
#include<bits/stdc++.h>
using namespace std;
const int N = 100005, SQ = 300;
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 36 ms 1920 KB Output is correct
4 Correct 26 ms 1456 KB Output is correct
5 Correct 32 ms 1792 KB Output is correct
6 Correct 32 ms 1792 KB Output is correct
7 Correct 25 ms 1792 KB Output is correct
8 Correct 25 ms 1920 KB Output is correct
9 Correct 27 ms 1792 KB Output is correct
10 Correct 24 ms 1792 KB Output is correct
11 Correct 25 ms 1792 KB Output is correct
12 Correct 25 ms 1792 KB Output is correct
13 Correct 32 ms 1912 KB Output is correct
14 Correct 30 ms 1792 KB Output is correct
15 Correct 34 ms 1888 KB Output is correct
16 Correct 26 ms 1792 KB Output is correct
17 Correct 26 ms 1784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1131 ms 7524 KB Output is correct
2 Correct 1049 ms 7580 KB Output is correct
3 Correct 1075 ms 7472 KB Output is correct
4 Correct 1198 ms 7936 KB Output is correct
5 Correct 1103 ms 7752 KB Output is correct
6 Correct 1195 ms 7604 KB Output is correct
7 Correct 1215 ms 7592 KB Output is correct
8 Correct 1216 ms 7488 KB Output is correct
9 Correct 154 ms 4216 KB Output is correct
10 Correct 985 ms 6664 KB Output is correct
11 Correct 965 ms 6388 KB Output is correct
12 Correct 1164 ms 7288 KB Output is correct
13 Correct 856 ms 7768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 772 ms 6644 KB Output is correct
2 Correct 475 ms 5112 KB Output is correct
3 Correct 839 ms 6392 KB Output is correct
4 Correct 764 ms 6612 KB Output is correct
5 Correct 156 ms 4216 KB Output is correct
6 Correct 825 ms 6820 KB Output is correct
7 Correct 743 ms 6392 KB Output is correct
8 Correct 707 ms 6524 KB Output is correct
9 Correct 853 ms 6656 KB Output is correct
10 Correct 767 ms 6524 KB Output is correct
11 Correct 559 ms 6560 KB Output is correct
12 Correct 529 ms 6436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 3089 ms 9092 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1131 ms 7524 KB Output is correct
2 Correct 1049 ms 7580 KB Output is correct
3 Correct 1075 ms 7472 KB Output is correct
4 Correct 1198 ms 7936 KB Output is correct
5 Correct 1103 ms 7752 KB Output is correct
6 Correct 1195 ms 7604 KB Output is correct
7 Correct 1215 ms 7592 KB Output is correct
8 Correct 1216 ms 7488 KB Output is correct
9 Correct 154 ms 4216 KB Output is correct
10 Correct 985 ms 6664 KB Output is correct
11 Correct 965 ms 6388 KB Output is correct
12 Correct 1164 ms 7288 KB Output is correct
13 Correct 856 ms 7768 KB Output is correct
14 Correct 772 ms 6644 KB Output is correct
15 Correct 475 ms 5112 KB Output is correct
16 Correct 839 ms 6392 KB Output is correct
17 Correct 764 ms 6612 KB Output is correct
18 Correct 156 ms 4216 KB Output is correct
19 Correct 825 ms 6820 KB Output is correct
20 Correct 743 ms 6392 KB Output is correct
21 Correct 707 ms 6524 KB Output is correct
22 Correct 853 ms 6656 KB Output is correct
23 Correct 767 ms 6524 KB Output is correct
24 Correct 559 ms 6560 KB Output is correct
25 Correct 529 ms 6436 KB Output is correct
26 Correct 1079 ms 7516 KB Output is correct
27 Correct 1695 ms 7396 KB Output is correct
28 Correct 1639 ms 7596 KB Output is correct
29 Correct 1505 ms 7644 KB Output is correct
30 Correct 1370 ms 7500 KB Output is correct
31 Correct 1412 ms 7608 KB Output is correct
32 Correct 1416 ms 7448 KB Output is correct
33 Correct 1260 ms 7520 KB Output is correct
34 Correct 1284 ms 7720 KB Output is correct
35 Correct 1318 ms 7612 KB Output is correct
36 Correct 1148 ms 7636 KB Output is correct
37 Correct 1142 ms 7484 KB Output is correct
38 Correct 1154 ms 7548 KB Output is correct
39 Correct 1157 ms 7596 KB Output is correct
40 Correct 1177 ms 7520 KB Output is correct
41 Correct 1200 ms 7676 KB Output is correct
42 Correct 868 ms 7552 KB Output is correct
43 Correct 900 ms 7528 KB Output is correct
44 Correct 882 ms 7428 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 36 ms 1920 KB Output is correct
4 Correct 26 ms 1456 KB Output is correct
5 Correct 32 ms 1792 KB Output is correct
6 Correct 32 ms 1792 KB Output is correct
7 Correct 25 ms 1792 KB Output is correct
8 Correct 25 ms 1920 KB Output is correct
9 Correct 27 ms 1792 KB Output is correct
10 Correct 24 ms 1792 KB Output is correct
11 Correct 25 ms 1792 KB Output is correct
12 Correct 25 ms 1792 KB Output is correct
13 Correct 32 ms 1912 KB Output is correct
14 Correct 30 ms 1792 KB Output is correct
15 Correct 34 ms 1888 KB Output is correct
16 Correct 26 ms 1792 KB Output is correct
17 Correct 26 ms 1784 KB Output is correct
18 Correct 1131 ms 7524 KB Output is correct
19 Correct 1049 ms 7580 KB Output is correct
20 Correct 1075 ms 7472 KB Output is correct
21 Correct 1198 ms 7936 KB Output is correct
22 Correct 1103 ms 7752 KB Output is correct
23 Correct 1195 ms 7604 KB Output is correct
24 Correct 1215 ms 7592 KB Output is correct
25 Correct 1216 ms 7488 KB Output is correct
26 Correct 154 ms 4216 KB Output is correct
27 Correct 985 ms 6664 KB Output is correct
28 Correct 965 ms 6388 KB Output is correct
29 Correct 1164 ms 7288 KB Output is correct
30 Correct 856 ms 7768 KB Output is correct
31 Correct 772 ms 6644 KB Output is correct
32 Correct 475 ms 5112 KB Output is correct
33 Correct 839 ms 6392 KB Output is correct
34 Correct 764 ms 6612 KB Output is correct
35 Correct 156 ms 4216 KB Output is correct
36 Correct 825 ms 6820 KB Output is correct
37 Correct 743 ms 6392 KB Output is correct
38 Correct 707 ms 6524 KB Output is correct
39 Correct 853 ms 6656 KB Output is correct
40 Correct 767 ms 6524 KB Output is correct
41 Correct 559 ms 6560 KB Output is correct
42 Correct 529 ms 6436 KB Output is correct
43 Execution timed out 3089 ms 9092 KB Time limit exceeded
44 Halted 0 ms 0 KB -