Submission #958236


Source Code Expand

#include <bits/stdc++.h>
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
 
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<int> vi;
typedef long double ld; 
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;
 
vi adj[100001];
int out[100001];
int in[100001];
vi type[100001];
int color[100001];
bool used2 = false;
int cnt = 0;
int c[3];
ll mm = 0;
ll tote = 0;
bool dfs(int u, int col)
{
	used2|=(in[u]&&out[u]);
	cnt++; c[col]++;
	color[u] = col;
	bool res = 1;
	mm += out[u];
	tote++;
	for(int i = 0; i < adj[u].size(); i++)
	{
		int v = adj[u][i];
		int nw = (col+type[u][i])%3;
		if(color[v] == -1)
		{
			res&=dfs(v, nw);
		}
		else
		{
			if(color[v] != nw) res=false;
		}
	}
	return res;
}
 
int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n, m; cin>>n>>m;
	for(int i = 0; i < m; i++)
	{
		int u, v; cin>>u>>v; u--; v--;
		adj[u].pb(v); adj[v].pb(u);
		type[u].pb(1); type[v].pb(2);
		out[u]++; in[v]++;
	}
	memset(color,-1,sizeof(color));
	ll res = 0;
	for(int i = 0; i < n; i++)
	{
		if(color[i] == -1)
		{
			used2 = false; cnt = 0;
			memset(c,0,sizeof(c));
			mm = 0; tote = 0;
			bool ans=dfs(i, 0);
			//cerr<<c[0]<<' '<<c[1]<<' '<<c[2]<<' '<<mm<<'\n';
			if(ans)
			{
				if(used2)
				{
					for(int j = 0; j < 3; j++)
					{
						res += ll(c[j])*ll(c[(j+1)%3]);
					}
				}
				else
				{
					res += mm;
				}
			}
			else
			{	
				res += ll(tote)*ll(tote);
			}
		}
	}
	cout<<res<<'\n';
}

Submission Info

Submission Time
Task F - Blackout
User zscoder
Language C++14 (GCC 5.4.1)
Score 1700
Code Size 1922 Byte
Status AC
Exec Time 72 ms
Memory 18560 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1700 / 1700
Status
AC × 3
AC × 53
Set Name Test Cases
Sample 0_00.txt, 0_01.txt, 0_02.txt
All 0_00.txt, 0_01.txt, 0_02.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt, 1_30.txt, 1_31.txt, 1_32.txt, 1_33.txt, 1_34.txt, 1_35.txt, 1_36.txt, 1_37.txt, 1_38.txt, 1_39.txt, 1_40.txt, 1_41.txt, 1_42.txt, 1_43.txt, 1_44.txt, 1_45.txt, 1_46.txt, 1_47.txt, 1_48.txt, 1_49.txt
Case Name Status Exec Time Memory
0_00.txt AC 8 ms 5376 KB
0_01.txt AC 8 ms 5376 KB
0_02.txt AC 8 ms 5376 KB
1_00.txt AC 8 ms 5376 KB
1_01.txt AC 9 ms 5376 KB
1_02.txt AC 50 ms 12416 KB
1_03.txt AC 58 ms 18560 KB
1_04.txt AC 57 ms 18560 KB
1_05.txt AC 57 ms 18560 KB
1_06.txt AC 56 ms 18560 KB
1_07.txt AC 57 ms 18560 KB
1_08.txt AC 57 ms 18560 KB
1_09.txt AC 57 ms 18560 KB
1_10.txt AC 58 ms 18560 KB
1_11.txt AC 57 ms 18560 KB
1_12.txt AC 56 ms 18560 KB
1_13.txt AC 57 ms 18560 KB
1_14.txt AC 40 ms 12784 KB
1_15.txt AC 40 ms 12784 KB
1_16.txt AC 41 ms 13168 KB
1_17.txt AC 27 ms 7936 KB
1_18.txt AC 68 ms 13312 KB
1_19.txt AC 70 ms 13312 KB
1_20.txt AC 71 ms 13184 KB
1_21.txt AC 72 ms 13312 KB
1_22.txt AC 66 ms 13312 KB
1_23.txt AC 67 ms 13312 KB
1_24.txt AC 66 ms 13184 KB
1_25.txt AC 66 ms 13312 KB
1_26.txt AC 66 ms 13184 KB
1_27.txt AC 66 ms 13184 KB
1_28.txt AC 69 ms 13312 KB
1_29.txt AC 69 ms 13312 KB
1_30.txt AC 65 ms 13184 KB
1_31.txt AC 66 ms 13184 KB
1_32.txt AC 66 ms 13312 KB
1_33.txt AC 67 ms 13312 KB
1_34.txt AC 64 ms 12544 KB
1_35.txt AC 57 ms 11264 KB
1_36.txt AC 61 ms 12160 KB
1_37.txt AC 66 ms 12928 KB
1_38.txt AC 55 ms 10624 KB
1_39.txt AC 52 ms 10240 KB
1_40.txt AC 66 ms 13184 KB
1_41.txt AC 68 ms 13056 KB
1_42.txt AC 58 ms 11648 KB
1_43.txt AC 58 ms 11392 KB
1_44.txt AC 63 ms 12672 KB
1_45.txt AC 52 ms 9728 KB
1_46.txt AC 65 ms 12800 KB
1_47.txt AC 67 ms 13056 KB
1_48.txt AC 57 ms 11008 KB
1_49.txt AC 61 ms 12032 KB