Submission #180587


Source Code Expand

#include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <functional>
#include <iterator>
#include <limits>
#include <numeric>
#include <utility>
#include <cmath>

using namespace std; using namespace placeholders;

using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VS = vector<string>;
using SS = stringstream;
using PII = pair<int,int>;
using VPII = vector< pair<int,int> >;
template < typename T = int > using VT = vector<T>;
template < typename T = int > using VVT = VT< VT<T> >;
template < typename T = int > using LIM = numeric_limits<T>;

template < typename T > inline T fromString( const string &s ){ T res; istringstream iss( s ); iss >> res; return res; };
template < typename T > inline string toString( const T &a ){ ostringstream oss; oss << a; return oss.str(); };

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i )
#define FOR( e, c ) for ( auto &e : c )
#define ALL( c ) (c).begin(), (c).end()
#define AALL( a, t ) (t*)a, (t*)a + sizeof( a ) / sizeof( t )
#define DRANGE( c, p ) (c).begin(), (c).begin() + p, (c).end()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl

// 最大流( Dinic 法 ) O( |E||V|^2 )
class Dinic
{
	struct Edge
	{
		int to, cap, rev;
		Edge( int t, int c, int r ) : to( t ), cap( c ), rev( r ) {}
	};

	vector< vector<Edge> > G;
	vector<int> distance, done;

public:
	Dinic( int V ) : G( V ), distance( V ), done( V )
	{
		return;
	}

	void connect( int from, int to, int cost )
	{
		G[ from ].push_back( Edge( to, cost, G[ to ].size() ) );
		G[ to ].push_back( Edge( from, 0, G[ from ].size() - 1 ) );
		return;
	}

	int solve( int s, int t )
	{
		int res = 0;
		while ( true )
		{
			bfs( s );
			if ( distance[t] < 0 )
			{
				return res;
			}

			fill( done.begin(), done.end(), 0 );
			for ( int f; ( f = dfs( s, t, numeric_limits<int>::max() ) ) > 0; res += f );
		}
	}

private:
	void bfs( int s )
	{
		fill( distance.begin(), distance.end(), -1 );
		distance[s] = 0;
		
		queue<int> que;
		que.push( s );
		while ( !que.empty() )
		{
			int v = que.front();
			que.pop();

			for ( int i = 0; i < (int)G[v].size(); ++i )
			{
				Edge &e = G[v][i];
				if ( e.cap > 0 && distance[ e.to ] < 0 )
				{
					distance[ e.to ] = distance[v] + 1;
					que.push( e.to );
				}
			}
		}

		return;
	}

	int dfs( int v, int t, int f )
	{
		if ( v == t )
		{
			return f;
		}

		for ( int &i = done[v]; i < (int)G[v].size(); ++i )
		{
			Edge &e = G[v][i];
			if ( e.cap > 0 && distance[v] < distance[ e.to ] )
			{
				int d = dfs( e.to, t, min( f, e.cap ) );
				if ( d > 0 )
				{
					e.cap -= d;
					G[ e.to ][ e.rev ].cap += d;
					return d;
				}
			}
		}

		return 0;
	}
};
// Dinic( |V| )
// connect( from, to, cap )
// solve( s, t )

int main()
{
	cin.tie( 0 );
	ios::sync_with_stdio( false );

	int n, g, e;
	cin >> n >> g >> e;

	VI ps( g );
	FOR( p, ps )
	{
		cin >> p;
	}

	Dinic maxflow( n + 2 );
	const int SRC = n;
	const int SINK = SRC + 1;

	maxflow.connect( SRC, 0, n );

	REP( iteraton, 0, e )
	{
		int a, b;
		cin >> a >> b;

		maxflow.connect( a, b, 1 );
		maxflow.connect( b, a, 1 );
	}

	FOR( p, ps )
	{
		maxflow.connect( p, SINK, 1 );
	}

	cout << maxflow.solve( SRC, SINK ) << endl;

	return 0;
}

Submission Info

Submission Time
Task D - 浮気予防
User torus711
Language C++11 (GCC 4.8.1)
Score 100
Code Size 3741 Byte
Status AC
Exec Time 49 ms
Memory 1232 KB

Judge Result

Set Name part All
Score / Max Score 99 / 99 1 / 1
Status
AC × 27
AC × 56
Set Name Test Cases
part test_01_AB.txt, test_02_AB.txt, test_03_AB.txt, test_04_AB.txt, test_05_AB.txt, test_06_AB.txt, test_07_AB.txt, test_08_AB.txt, test_09_AB.txt, test_10_AB.txt, test_11_AB.txt, test_12_AB.txt, test_13_AB.txt, test_14_AB.txt, test_15_AB.txt, test_16_AB.txt, test_17_AB.txt, test_18_AB.txt, test_19_AB.txt, test_20_AB.txt, test_21_AB.txt, test_22_AB.txt, test_23_AB.txt, test_24_AB.txt, test_25_AB.txt, test_41_AB.txt, test_47_AB.txt
All sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, sample_05.txt, test_01_AB.txt, test_02_AB.txt, test_03_AB.txt, test_04_AB.txt, test_05_AB.txt, test_06_AB.txt, test_07_AB.txt, test_08_AB.txt, test_09_AB.txt, test_10_AB.txt, test_11_AB.txt, test_12_AB.txt, test_13_AB.txt, test_14_AB.txt, test_15_AB.txt, test_16_AB.txt, test_17_AB.txt, test_18_AB.txt, test_19_AB.txt, test_20_AB.txt, test_21_AB.txt, test_22_AB.txt, test_23_AB.txt, test_24_AB.txt, test_25_AB.txt, test_26_A.txt, test_27_A.txt, test_28_A.txt, test_29_A.txt, test_30_A.txt, test_31_A.txt, test_32_A.txt, test_33_A.txt, test_34_A.txt, test_35_A.txt, test_36_A.txt, test_37_A.txt, test_38_A.txt, test_39_A.txt, test_40_A.txt, test_41_AB.txt, test_42_A.txt, test_43_A.txt, test_44_A.txt, test_45_A.txt, test_46_A.txt, test_47_AB.txt, test_48_A.txt, test_49_A.txt, test_50_A.txt, test_51_A.txt
Case Name Status Exec Time Memory
sample_01.txt AC 44 ms 792 KB
sample_02.txt AC 22 ms 920 KB
sample_03.txt AC 24 ms 932 KB
sample_04.txt AC 23 ms 916 KB
sample_05.txt AC 23 ms 912 KB
test_01_AB.txt AC 21 ms 916 KB
test_02_AB.txt AC 23 ms 920 KB
test_03_AB.txt AC 21 ms 920 KB
test_04_AB.txt AC 23 ms 924 KB
test_05_AB.txt AC 22 ms 732 KB
test_06_AB.txt AC 22 ms 732 KB
test_07_AB.txt AC 24 ms 920 KB
test_08_AB.txt AC 21 ms 928 KB
test_09_AB.txt AC 24 ms 924 KB
test_10_AB.txt AC 23 ms 916 KB
test_11_AB.txt AC 22 ms 732 KB
test_12_AB.txt AC 22 ms 924 KB
test_13_AB.txt AC 21 ms 728 KB
test_14_AB.txt AC 20 ms 912 KB
test_15_AB.txt AC 21 ms 924 KB
test_16_AB.txt AC 23 ms 924 KB
test_17_AB.txt AC 22 ms 940 KB
test_18_AB.txt AC 22 ms 836 KB
test_19_AB.txt AC 22 ms 732 KB
test_20_AB.txt AC 22 ms 920 KB
test_21_AB.txt AC 24 ms 940 KB
test_22_AB.txt AC 22 ms 920 KB
test_23_AB.txt AC 22 ms 920 KB
test_24_AB.txt AC 24 ms 936 KB
test_25_AB.txt AC 22 ms 920 KB
test_26_A.txt AC 24 ms 1152 KB
test_27_A.txt AC 26 ms 1128 KB
test_28_A.txt AC 24 ms 1088 KB
test_29_A.txt AC 24 ms 1116 KB
test_30_A.txt AC 24 ms 1176 KB
test_31_A.txt AC 26 ms 1232 KB
test_32_A.txt AC 22 ms 920 KB
test_33_A.txt AC 23 ms 1052 KB
test_34_A.txt AC 22 ms 916 KB
test_35_A.txt AC 24 ms 1024 KB
test_36_A.txt AC 22 ms 924 KB
test_37_A.txt AC 25 ms 976 KB
test_38_A.txt AC 24 ms 816 KB
test_39_A.txt AC 49 ms 792 KB
test_40_A.txt AC 22 ms 920 KB
test_41_AB.txt AC 22 ms 924 KB
test_42_A.txt AC 25 ms 980 KB
test_43_A.txt AC 22 ms 920 KB
test_44_A.txt AC 24 ms 976 KB
test_45_A.txt AC 22 ms 860 KB
test_46_A.txt AC 22 ms 924 KB
test_47_AB.txt AC 24 ms 916 KB
test_48_A.txt AC 24 ms 864 KB
test_49_A.txt AC 20 ms 920 KB
test_50_A.txt AC 23 ms 920 KB
test_51_A.txt AC 21 ms 912 KB