結果

問題 No.2464 To DAG
ユーザー kotatsugamekotatsugame
提出日時 2023-09-08 22:47:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,659 bytes
コンパイル時間 2,823 ms
コンパイル使用メモリ 76,716 KB
実行使用メモリ 33,152 KB
最終ジャッジ日時 2023-09-08 22:47:53
合計ジャッジ時間 20,746 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
12,604 KB
testcase_01 AC 8 ms
12,540 KB
testcase_02 AC 242 ms
29,528 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 246 ms
25,564 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 8 ms
12,528 KB
testcase_20 AC 9 ms
12,580 KB
testcase_21 AC 9 ms
12,652 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 53 ms
16,196 KB
testcase_27 AC 420 ms
33,152 KB
testcase_28 AC 390 ms
33,152 KB
testcase_29 AC 408 ms
32,072 KB
testcase_30 AC 278 ms
24,984 KB
testcase_31 AC 207 ms
23,696 KB
testcase_32 AC 185 ms
23,312 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 14 ms
16,068 KB
testcase_40 AC 14 ms
16,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<algorithm>
#include<vector>
#include<cassert>
using namespace std;
int N,M;
int U[6<<17],V[6<<17];
int deg[3<<17];
bool del[3<<17];
vector<int>G[3<<17];
int Gi[3<<17];
int Esi[3<<17];
void solve()
{
	for(int i=0;i<N;i++)
	{
		deg[i]=0;
		G[i].clear();
		Gi[i]=0;
		Esi[i]=-1;
	}
	for(int i=0;i<M;i++)if(!del[i])
	{
		deg[U[i]]++;
		deg[V[i]]--;
		G[U[i]].push_back(i);
	}
	vector<int>from,to;
	for(int i=0;i<N;i++)
	{
		if(deg[i]>=0)
		{
			for(int j=0;j<deg[i];j++)to.push_back(i);
		}
		else
		{
			for(int j=0;j<-deg[i];j++)from.push_back(i);
		}
	}
	assert(to.size()==from.size());
	for(int i=0;i<to.size();i++)
	{
		int sz=M+i;
		U[sz]=from[i];
		V[sz]=to[i];
		G[from[i]].push_back(sz);
	}
	vector<int>Es;
	bool ch=false;
	for(int i=0;i<N;i++)while(Gi[i]<G[i].size()&&G[i][Gi[i]]<M)
	{
		int u=i;
		Es.clear();
		do{
			assert(Gi[u]<G[u].size());
			int ei=G[u][Gi[u]++];
			Esi[u]=Es.size();
			Es.push_back(ei);
			u=V[ei];
			if(Esi[u]!=-1)
			{
				bool D=true;
				int L=Esi[u];
				for(int j=L;j<Es.size();j++)
				{
					if(Es[j]>=M)D=false;
				}
				if(D)
				{
					for(int j=L;j<Es.size();j++)del[Es[j]]=true;
					ch=true;
				}
				while(L<Es.size())
				{
					int ei=Es.back();
					Es.pop_back();
					Esi[U[ei]]=-1;
				}
			}
		}while(u!=i);
	}
	if(ch)solve();
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin>>N>>M;
	for(int i=0;i<M;i++)
	{
		cin>>U[i]>>V[i];
		U[i]--,V[i]--;
	}
	solve();
	vector<pair<int,int> >ans;
	for(int i=0;i<M;i++)if(!del[i])ans.push_back(make_pair(U[i],V[i]));
	cout<<N<<" "<<ans.size()<<"\n";
	for(pair<int,int>e:ans)cout<<e.first+1<<" "<<e.second+1<<"\n";
}
0