//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define trav(x,a) for (const auto& x: a)
#define sz(x) (int)(x).size()
#define mem(a,v) memset((a), (v), sizeof (a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%I64d", &(n))
#define nai(a, n) for (int _i = 1; _i <= (n); _i++) ni(a[_i])
#define nal(a, n) for (int _i = 1; _i <= (n); _i++) nl(a[_i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%I64d\n", (n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define st first
#define nd second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
const int N = 1e6 + 5;
const double eps = 1e-9;
int par[N];
set<int> eIn[N], eOut[N], comp[N];
ll ans, sz[N];
int qry(int x) { return x == par[x] ? x : par[x] = qry(par[x]); }
void upd(int u, int f) { ans += f * sz[u] * 1ll * (sz(comp[u]) - 1); }
deque<pii> pq;
void join(int u, int v) {
{
if (eIn[u].count(v))
eIn[u].erase(eIn[u].find(v));
if (eIn[v].count(u))
eIn[v].erase(eIn[v].find(u));
if (eOut[u].count(v))
eOut[u].erase(eOut[u].find(v));
if (eOut[v].count(u))
eOut[v].erase(eOut[v].find(u));
}
if (sz(comp[u]) > sz(comp[v]))
swap(u, v);
par[u] = v;
sz[v] += sz[u];
trav(x, comp[u]) comp[v].insert(x);
trav(x, eIn[u]) {
if (eOut[v].count(x))
pq.pb({x, v});
eOut[x].erase(eOut[x].find(u));
eOut[x].insert(v);
eIn[v].insert(x);
}
trav(x, eOut[u]) {
if (eIn[v].count(x))
pq.pb({x, v});
eIn[x].erase(eIn[x].find(u));
eIn[x].insert(v);
eOut[v].insert(x);
}
eIn[u].clear();
eOut[u].clear();
comp[u].clear();
}
int main() {
int n, m; scanf("%d %d", &n, &m);
iota(par, par+n+1, 0);
fill(sz, sz+n+1, 1);
for (int i = 1; i <= n ; i++)
comp[i].insert(i);
while (m--) {
int u, v; scanf("%d %d", &u, &v);
if (eOut[qry(v)].count(qry(u))) {
pq.pb({u, v});
while (!pq.empty()) {
tie(u, v) = pq.front();
pq.pop_front();
if (qry(u) == qry(v))
continue;
upd(qry(u), -1);
upd(qry(v), -1);
join(qry(u), qry(v));
upd(qry(u), 1);
}
} else if (qry(u) != qry(v)) {
upd(qry(v), -1);
comp[qry(v)].insert(u);
eIn[qry(v)].insert(qry(u));
eOut[qry(u)].insert(qry(v));
upd(qry(v), 1);
}
printf("%lld\n", ans);
}
return 0;
}
Compilation message
joitter2.cpp: In function 'int main()':
joitter2.cpp:82:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int n, m; scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
joitter2.cpp:88:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int u, v; scanf("%d %d", &u, &v);
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
141176 KB |
Output is correct |
2 |
Correct |
83 ms |
141304 KB |
Output is correct |
3 |
Correct |
79 ms |
141176 KB |
Output is correct |
4 |
Correct |
81 ms |
141176 KB |
Output is correct |
5 |
Correct |
84 ms |
141176 KB |
Output is correct |
6 |
Correct |
78 ms |
141280 KB |
Output is correct |
7 |
Correct |
80 ms |
141304 KB |
Output is correct |
8 |
Correct |
82 ms |
141316 KB |
Output is correct |
9 |
Correct |
79 ms |
141304 KB |
Output is correct |
10 |
Correct |
84 ms |
141292 KB |
Output is correct |
11 |
Correct |
79 ms |
141176 KB |
Output is correct |
12 |
Correct |
78 ms |
141176 KB |
Output is correct |
13 |
Correct |
78 ms |
141180 KB |
Output is correct |
14 |
Correct |
84 ms |
141304 KB |
Output is correct |
15 |
Correct |
84 ms |
141176 KB |
Output is correct |
16 |
Correct |
78 ms |
141308 KB |
Output is correct |
17 |
Correct |
78 ms |
141288 KB |
Output is correct |
18 |
Correct |
80 ms |
141176 KB |
Output is correct |
19 |
Correct |
81 ms |
141176 KB |
Output is correct |
20 |
Correct |
82 ms |
141304 KB |
Output is correct |
21 |
Correct |
80 ms |
141256 KB |
Output is correct |
22 |
Correct |
79 ms |
141304 KB |
Output is correct |
23 |
Correct |
84 ms |
141304 KB |
Output is correct |
24 |
Correct |
81 ms |
141304 KB |
Output is correct |
25 |
Correct |
82 ms |
141304 KB |
Output is correct |
26 |
Correct |
82 ms |
141176 KB |
Output is correct |
27 |
Correct |
78 ms |
141176 KB |
Output is correct |
28 |
Correct |
87 ms |
141320 KB |
Output is correct |
29 |
Correct |
82 ms |
141176 KB |
Output is correct |
30 |
Correct |
80 ms |
141176 KB |
Output is correct |
31 |
Correct |
79 ms |
141304 KB |
Output is correct |
32 |
Correct |
80 ms |
141308 KB |
Output is correct |
33 |
Correct |
81 ms |
141304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
141176 KB |
Output is correct |
2 |
Correct |
83 ms |
141304 KB |
Output is correct |
3 |
Correct |
79 ms |
141176 KB |
Output is correct |
4 |
Correct |
81 ms |
141176 KB |
Output is correct |
5 |
Correct |
84 ms |
141176 KB |
Output is correct |
6 |
Correct |
78 ms |
141280 KB |
Output is correct |
7 |
Correct |
80 ms |
141304 KB |
Output is correct |
8 |
Correct |
82 ms |
141316 KB |
Output is correct |
9 |
Correct |
79 ms |
141304 KB |
Output is correct |
10 |
Correct |
84 ms |
141292 KB |
Output is correct |
11 |
Correct |
79 ms |
141176 KB |
Output is correct |
12 |
Correct |
78 ms |
141176 KB |
Output is correct |
13 |
Correct |
78 ms |
141180 KB |
Output is correct |
14 |
Correct |
84 ms |
141304 KB |
Output is correct |
15 |
Correct |
84 ms |
141176 KB |
Output is correct |
16 |
Correct |
78 ms |
141308 KB |
Output is correct |
17 |
Correct |
78 ms |
141288 KB |
Output is correct |
18 |
Correct |
80 ms |
141176 KB |
Output is correct |
19 |
Correct |
81 ms |
141176 KB |
Output is correct |
20 |
Correct |
82 ms |
141304 KB |
Output is correct |
21 |
Correct |
80 ms |
141256 KB |
Output is correct |
22 |
Correct |
79 ms |
141304 KB |
Output is correct |
23 |
Correct |
84 ms |
141304 KB |
Output is correct |
24 |
Correct |
81 ms |
141304 KB |
Output is correct |
25 |
Correct |
82 ms |
141304 KB |
Output is correct |
26 |
Correct |
82 ms |
141176 KB |
Output is correct |
27 |
Correct |
78 ms |
141176 KB |
Output is correct |
28 |
Correct |
87 ms |
141320 KB |
Output is correct |
29 |
Correct |
82 ms |
141176 KB |
Output is correct |
30 |
Correct |
80 ms |
141176 KB |
Output is correct |
31 |
Correct |
79 ms |
141304 KB |
Output is correct |
32 |
Correct |
80 ms |
141308 KB |
Output is correct |
33 |
Correct |
81 ms |
141304 KB |
Output is correct |
34 |
Correct |
82 ms |
141304 KB |
Output is correct |
35 |
Correct |
213 ms |
144760 KB |
Output is correct |
36 |
Correct |
239 ms |
147064 KB |
Output is correct |
37 |
Correct |
238 ms |
147192 KB |
Output is correct |
38 |
Correct |
227 ms |
146936 KB |
Output is correct |
39 |
Correct |
82 ms |
141432 KB |
Output is correct |
40 |
Correct |
83 ms |
141560 KB |
Output is correct |
41 |
Correct |
85 ms |
141560 KB |
Output is correct |
42 |
Correct |
84 ms |
141432 KB |
Output is correct |
43 |
Correct |
88 ms |
141560 KB |
Output is correct |
44 |
Correct |
80 ms |
141560 KB |
Output is correct |
45 |
Correct |
80 ms |
141436 KB |
Output is correct |
46 |
Correct |
83 ms |
141432 KB |
Output is correct |
47 |
Correct |
82 ms |
141560 KB |
Output is correct |
48 |
Correct |
85 ms |
141640 KB |
Output is correct |
49 |
Correct |
91 ms |
142072 KB |
Output is correct |
50 |
Correct |
230 ms |
147192 KB |
Output is correct |
51 |
Correct |
88 ms |
141688 KB |
Output is correct |
52 |
Correct |
218 ms |
145676 KB |
Output is correct |
53 |
Correct |
95 ms |
142072 KB |
Output is correct |
54 |
Correct |
226 ms |
146408 KB |
Output is correct |
55 |
Correct |
93 ms |
141944 KB |
Output is correct |
56 |
Correct |
86 ms |
141944 KB |
Output is correct |
57 |
Correct |
86 ms |
141944 KB |
Output is correct |
58 |
Correct |
86 ms |
141944 KB |
Output is correct |
59 |
Correct |
85 ms |
141432 KB |
Output is correct |
60 |
Correct |
216 ms |
143736 KB |
Output is correct |
61 |
Correct |
95 ms |
141688 KB |
Output is correct |
62 |
Correct |
227 ms |
146680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
141176 KB |
Output is correct |
2 |
Correct |
83 ms |
141304 KB |
Output is correct |
3 |
Correct |
79 ms |
141176 KB |
Output is correct |
4 |
Correct |
81 ms |
141176 KB |
Output is correct |
5 |
Correct |
84 ms |
141176 KB |
Output is correct |
6 |
Correct |
78 ms |
141280 KB |
Output is correct |
7 |
Correct |
80 ms |
141304 KB |
Output is correct |
8 |
Correct |
82 ms |
141316 KB |
Output is correct |
9 |
Correct |
79 ms |
141304 KB |
Output is correct |
10 |
Correct |
84 ms |
141292 KB |
Output is correct |
11 |
Correct |
79 ms |
141176 KB |
Output is correct |
12 |
Correct |
78 ms |
141176 KB |
Output is correct |
13 |
Correct |
78 ms |
141180 KB |
Output is correct |
14 |
Correct |
84 ms |
141304 KB |
Output is correct |
15 |
Correct |
84 ms |
141176 KB |
Output is correct |
16 |
Correct |
78 ms |
141308 KB |
Output is correct |
17 |
Correct |
78 ms |
141288 KB |
Output is correct |
18 |
Correct |
80 ms |
141176 KB |
Output is correct |
19 |
Correct |
81 ms |
141176 KB |
Output is correct |
20 |
Correct |
82 ms |
141304 KB |
Output is correct |
21 |
Correct |
80 ms |
141256 KB |
Output is correct |
22 |
Correct |
79 ms |
141304 KB |
Output is correct |
23 |
Correct |
84 ms |
141304 KB |
Output is correct |
24 |
Correct |
81 ms |
141304 KB |
Output is correct |
25 |
Correct |
82 ms |
141304 KB |
Output is correct |
26 |
Correct |
82 ms |
141176 KB |
Output is correct |
27 |
Correct |
78 ms |
141176 KB |
Output is correct |
28 |
Correct |
87 ms |
141320 KB |
Output is correct |
29 |
Correct |
82 ms |
141176 KB |
Output is correct |
30 |
Correct |
80 ms |
141176 KB |
Output is correct |
31 |
Correct |
79 ms |
141304 KB |
Output is correct |
32 |
Correct |
80 ms |
141308 KB |
Output is correct |
33 |
Correct |
81 ms |
141304 KB |
Output is correct |
34 |
Correct |
82 ms |
141304 KB |
Output is correct |
35 |
Correct |
213 ms |
144760 KB |
Output is correct |
36 |
Correct |
239 ms |
147064 KB |
Output is correct |
37 |
Correct |
238 ms |
147192 KB |
Output is correct |
38 |
Correct |
227 ms |
146936 KB |
Output is correct |
39 |
Correct |
82 ms |
141432 KB |
Output is correct |
40 |
Correct |
83 ms |
141560 KB |
Output is correct |
41 |
Correct |
85 ms |
141560 KB |
Output is correct |
42 |
Correct |
84 ms |
141432 KB |
Output is correct |
43 |
Correct |
88 ms |
141560 KB |
Output is correct |
44 |
Correct |
80 ms |
141560 KB |
Output is correct |
45 |
Correct |
80 ms |
141436 KB |
Output is correct |
46 |
Correct |
83 ms |
141432 KB |
Output is correct |
47 |
Correct |
82 ms |
141560 KB |
Output is correct |
48 |
Correct |
85 ms |
141640 KB |
Output is correct |
49 |
Correct |
91 ms |
142072 KB |
Output is correct |
50 |
Correct |
230 ms |
147192 KB |
Output is correct |
51 |
Correct |
88 ms |
141688 KB |
Output is correct |
52 |
Correct |
218 ms |
145676 KB |
Output is correct |
53 |
Correct |
95 ms |
142072 KB |
Output is correct |
54 |
Correct |
226 ms |
146408 KB |
Output is correct |
55 |
Correct |
93 ms |
141944 KB |
Output is correct |
56 |
Correct |
86 ms |
141944 KB |
Output is correct |
57 |
Correct |
86 ms |
141944 KB |
Output is correct |
58 |
Correct |
86 ms |
141944 KB |
Output is correct |
59 |
Correct |
85 ms |
141432 KB |
Output is correct |
60 |
Correct |
216 ms |
143736 KB |
Output is correct |
61 |
Correct |
95 ms |
141688 KB |
Output is correct |
62 |
Correct |
227 ms |
146680 KB |
Output is correct |
63 |
Correct |
650 ms |
191444 KB |
Output is correct |
64 |
Correct |
650 ms |
191552 KB |
Output is correct |
65 |
Correct |
649 ms |
191480 KB |
Output is correct |
66 |
Correct |
302 ms |
149240 KB |
Output is correct |
67 |
Correct |
573 ms |
155616 KB |
Output is correct |
68 |
Correct |
296 ms |
149240 KB |
Output is correct |
69 |
Correct |
528 ms |
156532 KB |
Output is correct |
70 |
Correct |
332 ms |
149240 KB |
Output is correct |
71 |
Correct |
325 ms |
149240 KB |
Output is correct |
72 |
Correct |
559 ms |
155896 KB |
Output is correct |
73 |
Correct |
548 ms |
155640 KB |
Output is correct |
74 |
Correct |
1154 ms |
165624 KB |
Output is correct |
75 |
Correct |
766 ms |
161144 KB |
Output is correct |
76 |
Correct |
948 ms |
165240 KB |
Output is correct |
77 |
Correct |
917 ms |
165624 KB |
Output is correct |
78 |
Correct |
379 ms |
157968 KB |
Output is correct |
79 |
Correct |
551 ms |
159480 KB |
Output is correct |
80 |
Correct |
320 ms |
155512 KB |
Output is correct |
81 |
Correct |
501 ms |
157560 KB |
Output is correct |
82 |
Correct |
1083 ms |
176888 KB |
Output is correct |
83 |
Correct |
1108 ms |
176796 KB |
Output is correct |
84 |
Correct |
799 ms |
176760 KB |
Output is correct |
85 |
Correct |
815 ms |
176816 KB |
Output is correct |
86 |
Correct |
431 ms |
150264 KB |
Output is correct |
87 |
Correct |
460 ms |
151132 KB |
Output is correct |
88 |
Correct |
548 ms |
155640 KB |
Output is correct |
89 |
Correct |
884 ms |
165240 KB |
Output is correct |