結果

問題 No.1813 Magical Stones
ユーザー 👑 potato167potato167
提出日時 2022-01-14 22:29:55
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 144 ms / 2,000 ms
コード長 3,360 bytes
コンパイル時間 2,254 ms
コンパイル使用メモリ 208,804 KB
実行使用メモリ 17,224 KB
最終ジャッジ日時 2023-08-12 20:47:50
合計ジャッジ時間 6,090 ms
ジャッジサーバーID
(参考情報)
judge8 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 17 ms
14,056 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 17 ms
6,956 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 16 ms
14,060 KB
testcase_15 AC 137 ms
17,124 KB
testcase_16 AC 136 ms
17,224 KB
testcase_17 AC 144 ms
16,824 KB
testcase_18 AC 129 ms
17,132 KB
testcase_19 AC 139 ms
17,044 KB
testcase_20 AC 138 ms
17,032 KB
testcase_21 AC 135 ms
17,116 KB
testcase_22 AC 137 ms
17,032 KB
testcase_23 AC 133 ms
17,224 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 11 ms
5,652 KB
testcase_26 AC 4 ms
4,380 KB
testcase_27 AC 77 ms
13,404 KB
testcase_28 AC 103 ms
11,920 KB
testcase_29 AC 97 ms
14,016 KB
testcase_30 AC 92 ms
13,152 KB
testcase_31 AC 121 ms
16,484 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 4 ms
4,376 KB
testcase_35 AC 10 ms
4,384 KB
testcase_36 AC 3 ms
4,376 KB
testcase_37 AC 23 ms
5,276 KB
testcase_38 AC 16 ms
10,336 KB
testcase_39 AC 74 ms
13,804 KB
testcase_40 AC 3 ms
4,380 KB
testcase_41 AC 4 ms
4,376 KB
testcase_42 AC 10 ms
4,384 KB
testcase_43 AC 8 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define _GLIBCXX_DEBUG
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=1167167167167167167;
const int INF=2100000000;
const ll mod=1e9+7;
#define rep(i,a) for (ll i=0;i<a;i++)
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}



namespace po167{
struct SCC{
	std::vector<std::vector<int>> G;
	std::vector<std::vector<int>> G_rev;
	int size;
	SCC(int n):size(n),G(n),G_rev(n){}

	void add_edge(int from,int to){
		assert(0<=to&&to<size);
		assert(0<=from&&from<size);
		G[from].push_back(to);
		G_rev[to].push_back(from);
	}

	void dfs1(std::vector<int> &order,std::vector<int> &seen,int ind,int &now){
		if(seen[ind]!=0) return;
		seen[ind]=-1;
		for(auto x:G[ind]){
			dfs1(order,seen,x,now);
		}
		order[now]=ind;
		now++;
	}
	void dfs2(std::vector<int> &seen,int ind,int rank){
		if(seen[ind]!=-1) return;
		seen[ind]=rank;
		for(auto x:G_rev[ind]){
			dfs2(seen,x,rank);
		}
	}
	std::vector<std::vector<int>> tp_sort(){
		std::vector<int> seen(size,0),order(size);
		int now=0;
		for(int i=0;i<size;i++) dfs1(order,seen,i,now);
		now=0;
		for(int i=size-1;i>=0;i--){
			if(seen[order[i]]==-1){
				dfs2(seen,order[i],now);
				now++;
			}
		}
		std::vector<std::vector<int>> ans(now);
		for(int i=0;i<size;i++){
			ans[seen[i]].push_back(i);
		}
		return ans;
	}
	std::vector<int> for_two_sat(){
		std::vector<int> seen(size,0),order(size);
		int now=0;
		for(int i=0;i<size;i++) dfs1(order,seen,i,now);
		now=0;
		for(int i=size-1;i>=0;i--){
			if(seen[order[i]]==-1){
				dfs2(seen,order[i],now);
				now++;
			}
		}
		return seen;
	}
};

struct two_SAT{
	po167::SCC graph;
	std::vector<bool> ans;
	bool exists=false;
	int size;
	two_SAT(int n):size(n),ans(n),graph(2*n){}
	void add_clause(int i,bool f,int j,bool g){
		graph.add_edge(i+size*(!f),j+size*(g));
		graph.add_edge(j+size*(!g),i+size*(f));
	}
	bool build(){
		auto rank=graph.for_two_sat();
		exists=true;
		for(int i=0;i<size;i++){
			if(rank[i]==rank[i+size]){
				exists=false;
				break;
			}
			ans[i]=(rank[i]<rank[i+size]);
		}
		return exists;
	}
};
}
using po167::SCC;
using po167::two_SAT;

//  rainy ~ 雨に打たれて ~
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int N,M;
	cin>>N>>M;
	int A=0,B=0;
	SCC G(N);
	rep(i,M){
		int a,b;
		cin>>a>>b;
		a--,b--;
		G.add_edge(a,b);
	}
	auto p=G.tp_sort();
	vector<int> rev(N);
	rep(i,p.size()){
		for(auto y:p[i]) rev[y]=i;
	}
	rep(i,p.size()){
		int c=1,d=1;
		for(auto y:p[i]){
			for(auto x:G.G[y]){
				if(rev[y]<rev[x]) c=0;
			}
			for(auto x:G.G_rev[y]){
				if(rev[x]<rev[y]) d=0;
			}
		}
		if(c) A++;
		if(d) B++;
	}
	if(p.size()==1) cout<<"0\n";
	else cout<<max(A,B)<<"\n";
}

0