이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define sz(v) v.size()
#define pb push_back
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define x first
#define y second
#define int long long
#define nl "\n"
using namespace std;
typedef long long ll;
typedef pair<long long, long long> pll;
typedef pair <ll, ll> pii;
const int N = (int)3e5 + 7;
const int M = (int)15e6 + 7;
const ll MOD = (ll)1e9 + 7;
const int inf = (ll)1e9 + 7;
const ll INF = (ll)3e18 + 7;
pii dir[] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
int n, m, s, c, f, cur, d[N];
bool used[N], is;
vector<int> g[N];
bool dfs(int v, bool was) {
used[v] = 1;
if(v == c) was = 1;
if(was && v == f) return 1;
bool ok = 0;
for(auto to : g[v]) {
if(!used[to]) {
ok |= dfs(to, was);
if(ok) break;
}
}
used[v] = 0;
return ok;
}
void DFS(int v, int pr) {
used[v] = 1;
cur++;
for(auto to : g[v]) {
if(to == pr) continue;
if(used[to]) is = 1;
else {
DFS(to, v);
}
}
}
void solve() {
cin >> n >> m;
for(int i = 1; i <= m; ++i) {
int u, v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
bool ok = 1;
for(int i = 1; i <= n && ok; ++i) ok &= (sz(g[i]) <= 2);
int ans = 0;
if(n <= 10 && m <= 100) {
for(s = 1; s <= n; ++s) {
int cur = 0;
for(c = 1; c <= n; ++c) {
for(f = 1; f <= n; ++f) {
if(s != c && c != f && s != f) {
fill(used + 1, used + 1 + n, 0);
ans += dfs(s, 0);
}
}
} }
} else if(ok) {
for(int i = 1; i <= n; ++i) {
if(!used[i]) {
cur = is = 0;
DFS(i, 0);
if(is) ans += cur * (cur - 1) * (cur - 2);
else ans += cur * (cur - 1) * (cur - 2) / 3;
}
}
} else {
for(s = 1; s <= n; ++s) {
queue<int> q;
q.push(s);
fill(d + 1, d + 1 + n, inf);
d[s] = 0;
while(!q.empty()) {
int v = q.front();
q.pop();
for(auto to : g[v]) {
if(d[v] + 1 < d[to]) {
d[to] = d[v] + 1;
q.push(to);
}
}
}
for(int i = 1; i <= n; ++i) {
if(d[i] == inf || i == s) continue;
ans += (d[i] - 1);
}
}
}
cout << ans << nl;
}
signed main() {
ios_base::sync_with_stdio(NULL);
cin.tie(0);
cout.tie(0);
int test = 1;
//cin >> test;
for(int i = 1; i <= test; ++i) {
//cout << "Case " << i << ": ";
solve();
}
return 0;
}
/*
4 3
1 2
2 3
3 4
*/
컴파일 시 표준 에러 (stderr) 메시지
count_triplets.cpp: In function 'void solve()':
count_triplets.cpp:70:8: warning: unused variable 'cur' [-Wunused-variable]
70 | int cur = 0;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |