/* このコード、と~おれ! Be accepted! ∧_∧  (。・ω・。)つ━☆・*。 ⊂   ノ    ・゜+.  しーJ   °。+ *´¨)          .· ´¸.·*´¨) ¸.·*¨)           (¸.·´ (¸.·'* ☆ */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#pragma GCC target("arch=skylake-avx512") //#pragma GCC target ("avx2") //#pragma GCC optimize ("O3") //#pragma GCC target ("sse4") //#pragma GCC optimize ("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define repeat(i, n, m) for(int i = n; i < (m); ++i) #define rep(i, n) for(int i = 0; i < (n); ++i) #define printynl(a) printf(a ? "yes\n" : "no\n") #define printyn(a) printf(a ? "Yes\n" : "No\n") #define printYN(a) printf(a ? "YES\n" : "NO\n") #define printim(a) printf(a ? "possible\n" : "imposible\n") #define printdb(a) printf("%.50lf\n", a) #define printLdb(a) printf("%.50Lf\n", a) #define printdbd(a) printf("%.16lf\n", a) #define prints(s) printf("%s\n", s.c_str()) #define all(x) (x).begin(), (x).end() #define deg_to_rad(deg) (((deg)/360.0L)*2.0L*PI) #define rad_to_deg(rad) (((rad)/2.0L/PI)*360.0L) #define Please return #define AC 0 #define manhattan_dist(a, b, c, d) (abs(a - c) + abs(b - d)) using ll = long long; using ull = unsigned long long; constexpr int INF = 1073741823; constexpr int MINF = -1073741823; constexpr ll LINF = ll(4661686018427387903); constexpr ll MOD = 1e9 + 7; constexpr ll mod = 998244353; constexpr long double eps = 1e-6; const long double PI = acosl(-1.0L); using namespace std; void scans(string& str) { char c; str = ""; scanf("%c", &c); if (c == '\n')scanf("%c", &c); while (c != '\n' && c != -1 && c != ' ') { str += c; scanf("%c", &c); } } void scanc(char& str) { char c; scanf("%c", &c); if (c == -1)return; while (c == '\n') { scanf("%c", &c); } str = c; } double acot(double x) { return PI / 2 - atan(x); } ll LSB(ll n) { return (n & (-n)); } template inline T chmin(T& a, const T& b) { if (a > b)a = b; return a; } template inline T chmax(T& a, const T& b) { if (a < b)a = b; return a; } ////cpp_int //#include //#include //using namespace boost::multiprecision; //atcoder library //#include //using namespace atcoder; //random_device seed_gen; //mt19937 engine(seed_gen()); //uniform_distribution dist(-1.0, 1.0); /*----------------------------------------------------------------------------------*/ /* * @title unionfind * @docs kyopro/docs/unionfind.md */ //0-indexed struct unionfind { vector par, siz; unionfind(int n) : par(n), siz(n) { for (int i = 0; i < n; ++i) { //全部根で初期化 par[i] = i; //サイズは1 siz[i] = 1; } } void init(int n) { par.resize(n); siz.resize(n); for (int i = 0; i < n; ++i) { //全部根で初期化 par[i] = i; //サイズは1 siz[i] = 1; } } //根を返す int find(int a) { return par[a] == a ? a : par[a] = find(par[a]); } //くっつける。元から同じだったらfalseを返す bool unite(int a, int b) { int x = find(a), y = find(b); if (x == y)return false; else if (siz[x] < siz[y]) { par[x] = y; siz[y] += siz[x]; } else if (siz[x] > siz[y]) { par[y] = x; siz[x] += siz[y]; } else { par[x] = y; siz[y] += siz[x]; } return true; } //同じ集合か判定する bool same(int a, int b) { return find(a) == find(b); } //サイズを返す int size(int a) { return siz[find(a)]; } }; struct set_enumerate { int n; vector next; unionfind uni; set_enumerate(int siz) : n(siz), next(siz), uni(n) { next.assign(siz, 0); rep(i, siz)next[i] = i; } int size() { return n; } vector enumerate(const int x) { vector ret; int cur = x; do { ret.emplace_back(cur); cur = next[cur]; } while (cur != x); return ret; } vector> get_all() { vector> ret; vector visited(n); rep(i, n) { if (not visited[i]) { ret.emplace_back(enumerate(i)); for (const auto& i : ret.back())visited[i] = true; } } return ret; } void unite(const int x, const int y) { if (not uni.same(x, y)) swap(next[x], next[y]); uni.unite(x, y); } bool same(int a, int b) { return uni.same(a, b); } int find(int a) { return uni.find(a); } int size(int i) { return uni.size(i); } }; int main() { int n, d, w; scanf("%d%d%d", &n, &d, &w); set_enumerate car(n), walk(n); ll ans = 0; rep(i, d) { int a, b; scanf("%d%d", &a, &b); car.unite(a - 1, b - 1); } rep(i, w) { int a, b; scanf("%d%d", &a, &b); walk.unite(a - 1, b - 1); } vector flag(n); rep(i, n) { if (not flag[i]) { auto ushi = car.enumerate(i); ll m = 0, x = car.size(i); set b; for (const auto& aa : ushi)b.insert(walk.find(aa)), flag[aa] = true; for (const auto& aa : b)m += walk.size(aa); ans += m * x; flag[i] = true; } } printf("%lld\n", ans - (ll)n); Please AC; }