結果

問題 No.1605 Matrix Shape
ユーザー 👑 kmjpkmjp
提出日時 2021-07-16 22:04:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 139 ms / 2,000 ms
コード長 2,067 bytes
コンパイル時間 2,174 ms
コンパイル使用メモリ 204,776 KB
実行使用メモリ 27,764 KB
最終ジャッジ日時 2023-09-20 14:02:42
合計ジャッジ時間 5,484 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
13,212 KB
testcase_01 AC 8 ms
13,292 KB
testcase_02 AC 7 ms
13,316 KB
testcase_03 AC 7 ms
13,240 KB
testcase_04 AC 7 ms
13,284 KB
testcase_05 AC 8 ms
13,364 KB
testcase_06 AC 8 ms
13,376 KB
testcase_07 AC 7 ms
13,244 KB
testcase_08 AC 8 ms
13,312 KB
testcase_09 AC 7 ms
13,384 KB
testcase_10 AC 7 ms
13,312 KB
testcase_11 AC 7 ms
13,360 KB
testcase_12 AC 8 ms
13,188 KB
testcase_13 AC 7 ms
13,300 KB
testcase_14 AC 7 ms
13,368 KB
testcase_15 AC 7 ms
13,216 KB
testcase_16 AC 7 ms
13,292 KB
testcase_17 AC 7 ms
13,324 KB
testcase_18 AC 7 ms
13,208 KB
testcase_19 AC 139 ms
27,764 KB
testcase_20 AC 91 ms
21,060 KB
testcase_21 AC 88 ms
21,124 KB
testcase_22 AC 106 ms
24,408 KB
testcase_23 AC 131 ms
26,604 KB
testcase_24 AC 135 ms
27,696 KB
testcase_25 AC 46 ms
20,888 KB
testcase_26 AC 47 ms
20,780 KB
testcase_27 AC 46 ms
21,108 KB
testcase_28 AC 36 ms
14,332 KB
testcase_29 AC 46 ms
20,912 KB
testcase_30 AC 84 ms
18,280 KB
testcase_31 AC 89 ms
18,292 KB
testcase_32 AC 75 ms
20,712 KB
testcase_33 AC 62 ms
15,960 KB
testcase_34 AC 48 ms
20,880 KB
testcase_35 AC 65 ms
19,724 KB
testcase_36 AC 40 ms
17,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

template<int MV> class DirectedEulerPath {
public:
	int NV;
	vector<int> path;
	vector<int> E[MV];
	void add_path(int x,int y) { E[x].push_back(y);}
	
	void init(int NV){
		this->NV=NV;
		for(int i=0;i<NV;i++) E[i].clear();
		path.clear();
	}
	void dfs(int cur) {
		while(E[cur].size()) {
			int e=E[cur].back();
			E[cur].pop_back();
			dfs(e);
		}
		path.push_back(cur);
	}
	
	bool GetPath() {
		assert(NV);
		int te=0,i;
		vector<int> deg(NV);
		int s=-1;
		FOR(i,NV) {
			te += E[i].size();
			deg[i]+=E[i].size();
			FORR(e,E[i]) deg[e]--;
			if(E[i].size()) s=i;
		}
		if(s==-1) return 0;
		int d0=0;
		FOR(i,NV) {
			if(deg[i]>1 || deg[i]<-1) return false;
			if(deg[i]==0) d0++;
			if(deg[i]==1) s=i;
		}
		if(d0!=NV && d0+2!=NV) return false;
		dfs(s);
		reverse(path.begin(),path.end());
		return path.size()==te+1;
	}
};

int N;
vector<int> E[202020];
DirectedEulerPath<202020> dep;
int in[202020],out[202020];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	dep.init(200000);
	FOR(i,N) {
		cin>>x>>y;
		dep.add_path(x-1,y-1);
		out[x-1]++;
		in[y-1]++;
	}
	
	if(dep.GetPath()) {
		int is=0;
		FOR(i,200000) {
			if(in[i]!=out[i]) {
				cout<<1<<endl;
				return;
			}
			if(in[i]) is++;
		}
		cout<<is<<endl;
	}
	else {
		cout<<0<<endl;
	}
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0