Submission #516970

#TimeUsernameProblemLanguageResultExecution timeMemory
516970pragmatistDuathlon (APIO18_duathlon)C++17
23 / 100
1095 ms15252 KiB
#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
*/

Compilation message (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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...