#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 parent[100000],size[100000];
int find(int n) {
if (parent[n] != n) parent[n] = find(parent[n]);
return parent[n];
}
set<pii> in[100000],out[100000];
LLI ans = 0;
int merge(int a,int b) {
if (find(a) == find(b)) return 0;
if (in[a].size()+out[a].size() > in[b].size()+out[b].size()) swap(a,b);
ans -= (LLI) size[a]*in[a].size()+(LLI) size[b]*in[b].size();
vi v;
for (auto it = in[a].begin(); it != in[a].end(); it++) {
out[it->first].erase(mp(a,it->second));
if (it->first != b) {
in[b].insert(*it),out[it->first].insert(mp(b,it->second));
auto it2 = in[it->first].lower_bound(mp(b,0));
if ((it2 != in[it->first].end()) && (it2->first == b)) v.pb(it->first);
}
}
for (auto it = out[a].begin(); it != out[a].end(); it++) {
in[it->first].erase(mp(a,it->second));
if (it->first != b) {
out[b].insert(*it),in[it->first].insert(mp(b,it->second));
auto it2 = out[it->first].lower_bound(mp(b,0));
if ((it2 != out[it->first].end()) && (it2->first == b)) v.pb(it->first);
}
}
ans += (LLI) (size[a]+size[b])*in[b].size()+2LL*size[a]*size[b];
in[a].clear(),out[a].clear(),parent[a] = b,size[b] += size[a];
while (!v.empty()) merge(b,v.back()),v.pop_back();
return 0;
}
int main() {
int i;
int N,M,A,B;
scanf("%d %d",&N,&M);
for (i = 0; i < N; i++) parent[i] = i,size[i] = 1;
for (i = 0; i < M; i++) {
scanf("%d %d",&A,&B);
A--,B--;
if ((find(A) != find(B)) && !in[find(B)].count(mp(find(A),A))) {
in[find(B)].insert(mp(find(A),A)),out[find(A)].insert(mp(find(B),A));
ans += size[find(B)];
auto it = in[find(A)].lower_bound(mp(find(B),0));
if ((it != in[find(A)].end()) && (it->first == find(B))) merge(find(A),find(B));
}
printf("%lld\n",ans);
}
return 0;
}
Compilation message
joitter2.cpp: In function 'int main()':
joitter2.cpp:94:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&N,&M);
~~~~~^~~~~~~~~~~~~~~
joitter2.cpp:97:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&A,&B);
~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
9728 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
9 ms |
9728 KB |
Output is correct |
4 |
Correct |
11 ms |
9600 KB |
Output is correct |
5 |
Correct |
10 ms |
9728 KB |
Output is correct |
6 |
Correct |
10 ms |
9728 KB |
Output is correct |
7 |
Correct |
10 ms |
9728 KB |
Output is correct |
8 |
Correct |
11 ms |
9728 KB |
Output is correct |
9 |
Correct |
11 ms |
9728 KB |
Output is correct |
10 |
Correct |
10 ms |
9728 KB |
Output is correct |
11 |
Correct |
9 ms |
9728 KB |
Output is correct |
12 |
Correct |
10 ms |
9728 KB |
Output is correct |
13 |
Correct |
10 ms |
9728 KB |
Output is correct |
14 |
Correct |
10 ms |
9728 KB |
Output is correct |
15 |
Correct |
10 ms |
9728 KB |
Output is correct |
16 |
Correct |
10 ms |
9728 KB |
Output is correct |
17 |
Correct |
9 ms |
9728 KB |
Output is correct |
18 |
Correct |
10 ms |
9728 KB |
Output is correct |
19 |
Correct |
10 ms |
9704 KB |
Output is correct |
20 |
Correct |
11 ms |
9728 KB |
Output is correct |
21 |
Correct |
11 ms |
9728 KB |
Output is correct |
22 |
Correct |
10 ms |
9728 KB |
Output is correct |
23 |
Correct |
10 ms |
9728 KB |
Output is correct |
24 |
Correct |
10 ms |
9728 KB |
Output is correct |
25 |
Correct |
11 ms |
9856 KB |
Output is correct |
26 |
Correct |
10 ms |
9728 KB |
Output is correct |
27 |
Correct |
10 ms |
9728 KB |
Output is correct |
28 |
Correct |
10 ms |
9728 KB |
Output is correct |
29 |
Correct |
10 ms |
9728 KB |
Output is correct |
30 |
Correct |
10 ms |
9728 KB |
Output is correct |
31 |
Correct |
10 ms |
9728 KB |
Output is correct |
32 |
Correct |
10 ms |
9728 KB |
Output is correct |
33 |
Correct |
11 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
9728 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
9 ms |
9728 KB |
Output is correct |
4 |
Correct |
11 ms |
9600 KB |
Output is correct |
5 |
Correct |
10 ms |
9728 KB |
Output is correct |
6 |
Correct |
10 ms |
9728 KB |
Output is correct |
7 |
Correct |
10 ms |
9728 KB |
Output is correct |
8 |
Correct |
11 ms |
9728 KB |
Output is correct |
9 |
Correct |
11 ms |
9728 KB |
Output is correct |
10 |
Correct |
10 ms |
9728 KB |
Output is correct |
11 |
Correct |
9 ms |
9728 KB |
Output is correct |
12 |
Correct |
10 ms |
9728 KB |
Output is correct |
13 |
Correct |
10 ms |
9728 KB |
Output is correct |
14 |
Correct |
10 ms |
9728 KB |
Output is correct |
15 |
Correct |
10 ms |
9728 KB |
Output is correct |
16 |
Correct |
10 ms |
9728 KB |
Output is correct |
17 |
Correct |
9 ms |
9728 KB |
Output is correct |
18 |
Correct |
10 ms |
9728 KB |
Output is correct |
19 |
Correct |
10 ms |
9704 KB |
Output is correct |
20 |
Correct |
11 ms |
9728 KB |
Output is correct |
21 |
Correct |
11 ms |
9728 KB |
Output is correct |
22 |
Correct |
10 ms |
9728 KB |
Output is correct |
23 |
Correct |
10 ms |
9728 KB |
Output is correct |
24 |
Correct |
10 ms |
9728 KB |
Output is correct |
25 |
Correct |
11 ms |
9856 KB |
Output is correct |
26 |
Correct |
10 ms |
9728 KB |
Output is correct |
27 |
Correct |
10 ms |
9728 KB |
Output is correct |
28 |
Correct |
10 ms |
9728 KB |
Output is correct |
29 |
Correct |
10 ms |
9728 KB |
Output is correct |
30 |
Correct |
10 ms |
9728 KB |
Output is correct |
31 |
Correct |
10 ms |
9728 KB |
Output is correct |
32 |
Correct |
10 ms |
9728 KB |
Output is correct |
33 |
Correct |
11 ms |
9728 KB |
Output is correct |
34 |
Correct |
14 ms |
9856 KB |
Output is correct |
35 |
Correct |
129 ms |
15116 KB |
Output is correct |
36 |
Correct |
157 ms |
17272 KB |
Output is correct |
37 |
Correct |
152 ms |
17272 KB |
Output is correct |
38 |
Correct |
151 ms |
17180 KB |
Output is correct |
39 |
Correct |
12 ms |
9856 KB |
Output is correct |
40 |
Correct |
12 ms |
9856 KB |
Output is correct |
41 |
Correct |
12 ms |
9856 KB |
Output is correct |
42 |
Correct |
12 ms |
9856 KB |
Output is correct |
43 |
Correct |
13 ms |
9856 KB |
Output is correct |
44 |
Correct |
13 ms |
9856 KB |
Output is correct |
45 |
Correct |
12 ms |
9856 KB |
Output is correct |
46 |
Correct |
13 ms |
9984 KB |
Output is correct |
47 |
Correct |
14 ms |
9856 KB |
Output is correct |
48 |
Correct |
13 ms |
9984 KB |
Output is correct |
49 |
Correct |
20 ms |
10496 KB |
Output is correct |
50 |
Correct |
164 ms |
17524 KB |
Output is correct |
51 |
Correct |
17 ms |
10112 KB |
Output is correct |
52 |
Correct |
142 ms |
16432 KB |
Output is correct |
53 |
Correct |
20 ms |
10496 KB |
Output is correct |
54 |
Correct |
147 ms |
16760 KB |
Output is correct |
55 |
Correct |
14 ms |
10240 KB |
Output is correct |
56 |
Correct |
14 ms |
10240 KB |
Output is correct |
57 |
Correct |
15 ms |
10624 KB |
Output is correct |
58 |
Correct |
14 ms |
10624 KB |
Output is correct |
59 |
Correct |
12 ms |
9856 KB |
Output is correct |
60 |
Correct |
125 ms |
14688 KB |
Output is correct |
61 |
Correct |
14 ms |
9984 KB |
Output is correct |
62 |
Correct |
146 ms |
17016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
9728 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
9 ms |
9728 KB |
Output is correct |
4 |
Correct |
11 ms |
9600 KB |
Output is correct |
5 |
Correct |
10 ms |
9728 KB |
Output is correct |
6 |
Correct |
10 ms |
9728 KB |
Output is correct |
7 |
Correct |
10 ms |
9728 KB |
Output is correct |
8 |
Correct |
11 ms |
9728 KB |
Output is correct |
9 |
Correct |
11 ms |
9728 KB |
Output is correct |
10 |
Correct |
10 ms |
9728 KB |
Output is correct |
11 |
Correct |
9 ms |
9728 KB |
Output is correct |
12 |
Correct |
10 ms |
9728 KB |
Output is correct |
13 |
Correct |
10 ms |
9728 KB |
Output is correct |
14 |
Correct |
10 ms |
9728 KB |
Output is correct |
15 |
Correct |
10 ms |
9728 KB |
Output is correct |
16 |
Correct |
10 ms |
9728 KB |
Output is correct |
17 |
Correct |
9 ms |
9728 KB |
Output is correct |
18 |
Correct |
10 ms |
9728 KB |
Output is correct |
19 |
Correct |
10 ms |
9704 KB |
Output is correct |
20 |
Correct |
11 ms |
9728 KB |
Output is correct |
21 |
Correct |
11 ms |
9728 KB |
Output is correct |
22 |
Correct |
10 ms |
9728 KB |
Output is correct |
23 |
Correct |
10 ms |
9728 KB |
Output is correct |
24 |
Correct |
10 ms |
9728 KB |
Output is correct |
25 |
Correct |
11 ms |
9856 KB |
Output is correct |
26 |
Correct |
10 ms |
9728 KB |
Output is correct |
27 |
Correct |
10 ms |
9728 KB |
Output is correct |
28 |
Correct |
10 ms |
9728 KB |
Output is correct |
29 |
Correct |
10 ms |
9728 KB |
Output is correct |
30 |
Correct |
10 ms |
9728 KB |
Output is correct |
31 |
Correct |
10 ms |
9728 KB |
Output is correct |
32 |
Correct |
10 ms |
9728 KB |
Output is correct |
33 |
Correct |
11 ms |
9728 KB |
Output is correct |
34 |
Correct |
14 ms |
9856 KB |
Output is correct |
35 |
Correct |
129 ms |
15116 KB |
Output is correct |
36 |
Correct |
157 ms |
17272 KB |
Output is correct |
37 |
Correct |
152 ms |
17272 KB |
Output is correct |
38 |
Correct |
151 ms |
17180 KB |
Output is correct |
39 |
Correct |
12 ms |
9856 KB |
Output is correct |
40 |
Correct |
12 ms |
9856 KB |
Output is correct |
41 |
Correct |
12 ms |
9856 KB |
Output is correct |
42 |
Correct |
12 ms |
9856 KB |
Output is correct |
43 |
Correct |
13 ms |
9856 KB |
Output is correct |
44 |
Correct |
13 ms |
9856 KB |
Output is correct |
45 |
Correct |
12 ms |
9856 KB |
Output is correct |
46 |
Correct |
13 ms |
9984 KB |
Output is correct |
47 |
Correct |
14 ms |
9856 KB |
Output is correct |
48 |
Correct |
13 ms |
9984 KB |
Output is correct |
49 |
Correct |
20 ms |
10496 KB |
Output is correct |
50 |
Correct |
164 ms |
17524 KB |
Output is correct |
51 |
Correct |
17 ms |
10112 KB |
Output is correct |
52 |
Correct |
142 ms |
16432 KB |
Output is correct |
53 |
Correct |
20 ms |
10496 KB |
Output is correct |
54 |
Correct |
147 ms |
16760 KB |
Output is correct |
55 |
Correct |
14 ms |
10240 KB |
Output is correct |
56 |
Correct |
14 ms |
10240 KB |
Output is correct |
57 |
Correct |
15 ms |
10624 KB |
Output is correct |
58 |
Correct |
14 ms |
10624 KB |
Output is correct |
59 |
Correct |
12 ms |
9856 KB |
Output is correct |
60 |
Correct |
125 ms |
14688 KB |
Output is correct |
61 |
Correct |
14 ms |
9984 KB |
Output is correct |
62 |
Correct |
146 ms |
17016 KB |
Output is correct |
63 |
Correct |
481 ms |
44152 KB |
Output is correct |
64 |
Correct |
504 ms |
44236 KB |
Output is correct |
65 |
Correct |
488 ms |
44316 KB |
Output is correct |
66 |
Correct |
150 ms |
14968 KB |
Output is correct |
67 |
Correct |
253 ms |
18808 KB |
Output is correct |
68 |
Correct |
149 ms |
14968 KB |
Output is correct |
69 |
Correct |
319 ms |
19576 KB |
Output is correct |
70 |
Correct |
162 ms |
14968 KB |
Output is correct |
71 |
Correct |
151 ms |
14968 KB |
Output is correct |
72 |
Correct |
292 ms |
19064 KB |
Output is correct |
73 |
Correct |
271 ms |
18936 KB |
Output is correct |
74 |
Correct |
845 ms |
28504 KB |
Output is correct |
75 |
Correct |
486 ms |
24440 KB |
Output is correct |
76 |
Correct |
603 ms |
27512 KB |
Output is correct |
77 |
Correct |
590 ms |
27640 KB |
Output is correct |
78 |
Correct |
177 ms |
18680 KB |
Output is correct |
79 |
Correct |
325 ms |
22136 KB |
Output is correct |
80 |
Correct |
210 ms |
18404 KB |
Output is correct |
81 |
Correct |
309 ms |
21624 KB |
Output is correct |
82 |
Correct |
518 ms |
32888 KB |
Output is correct |
83 |
Correct |
524 ms |
32888 KB |
Output is correct |
84 |
Correct |
491 ms |
50168 KB |
Output is correct |
85 |
Correct |
503 ms |
50332 KB |
Output is correct |
86 |
Correct |
159 ms |
14072 KB |
Output is correct |
87 |
Correct |
204 ms |
16376 KB |
Output is correct |
88 |
Correct |
279 ms |
18980 KB |
Output is correct |
89 |
Correct |
583 ms |
26848 KB |
Output is correct |