Submission #474649

#TimeUsernameProblemLanguageResultExecution timeMemory
474649sean617우호 조약 체결 (JOI14_friends)C++98
0 / 100
85 ms13512 KiB
#include <iostream> #include <cstdio> #include <vector> #include <queue> #define N 100005 using namespace std; typedef long long ll; ll n, m, ans, bu[N], v[N], m1[N*2], m2[N*2], s[N]; vector<int> a[N]; queue<int> qu; ll f(ll p) { if (bu[p] == p) return p; else return bu[p] = f(bu[p]);; } int main() { ll i, j, t, t1, t2, x1, x2; ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (i = 0; i < m; i++) { cin >> t1 >> t2; m1[i] = t1; m2[i] = t2; a[t1].push_back(t2); } for (i = 1; i <= n; i++) bu[i] = i; for (i = 1; i <= n; i++) { if (a[i].size() >= 2) { for (j = 1; j < a[i].size(); j++) { x1 = f(a[i][0]); x2 = f(a[i][j]); if (x1 != x2) bu[x2] = x1; } } } for (i =1; i <= n; i++) { if (bu[i] != i) {v[i] =1; qu.push(i);} } while (!qu.empty()) { t = qu.front(); qu.pop(); for (i = 0; i < a[t].size(); i++) { t2 = a[t][i]; x1 = f(t); x2 = f(t2); if (x1 != x2) bu[x2] = x1; if (v[t2]) continue; qu.push(t2); v[t2] = 1; } } for (i = 1; i <= n; i++) f(i); for (i = 1; i <= n; i++) { s[bu[i]]++; } for (i = 1; i <= n; i++) { if (s[i] > 1) ans += s[i] * (s[i] - 1); } for (i = 0; i < m; i++) { if (bu[m1[i]] != bu[m2[i]]) ans++; } cout << ans; return 0; }

Compilation message (stderr)

friends.cpp: In function 'int main()':
friends.cpp:31:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |    for (j = 1; j < a[i].size(); j++) {
      |                ~~^~~~~~~~~~~~~
friends.cpp:43:17: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |   for (i = 0; i < a[t].size(); i++) {
      |               ~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...