結果
| 問題 |
No.1420 国勢調査 (Easy)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-03-05 22:42:27 |
| 言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 4,089 bytes |
| コンパイル時間 | 5,590 ms |
| コンパイル使用メモリ | 194,188 KB |
| 最終ジャッジ日時 | 2025-01-19 11:37:55 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 MLE * 1 |
| other | AC * 7 TLE * 14 MLE * 9 |
ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;
using ll = long long;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;
template<class T>
inline bool chmax(T& x, T y){
if(x < y){
x = y;
return true;
}
return false;
}
template<class T>
inline bool chmin(T& x, T y){
if(x > y){
x = y;
return true;
}
return false;
}
struct StronglyConnectedComponents{
using G = vector<vector<int>>;
G graph;
G rev;
int n;
int scc_sz = 0;
vector<int> scc_id;
vector<int> vs;
vector<bool> visited;
StronglyConnectedComponents() = default;
StronglyConnectedComponents(int V) : n(V) {
graph.resize(n);
rev.resize(n);
scc_id.assign(n, -1);
visited.assign(n, false);
}
StronglyConnectedComponents(G& g) : graph(g), n(g.size()) {
rev.resize(n);
scc_id.assign(n, -1);
visited.assign(n, false);
for(int u = 0; u < n; ++u){
for(int v : graph[u]) rev[v].push_back(u);
}
}
void resize(int V){
n = V;
graph.resize(n);
rev.resize(n);
scc_id.assign(n, -1);
visited.assign(n, false);
}
void add_edge(int u, int v){
graph[u].emplace_back(v);
rev[v].emplace_back(u);
}
void dfs(int from){
visited[from] = true;
for(int to : graph[from]){
if(!visited[to]) dfs(to);
}
vs.push_back(from);
}
void rdfs(int from, int k){
scc_id[from] = k;
for(int to : rev[from]){
if(scc_id[to] == -1) rdfs(to, k);
}
}
void build(){
for(int v = 0; v < n; ++v){
if(!visited[v]) dfs(v);
}
for(int i = n - 1; i >= 0; --i){
if(scc_id[vs[i]] == -1){
rdfs(vs[i], scc_sz++);
}
}
return;
}
int size() { return scc_sz; }
int operator[](int v) { return scc_id[v]; }
};
struct TwoSAT {
StronglyConnectedComponents scc;
int n;
vector<int> sat;
TwoSAT(int V) : n(V) {
scc.resize(n * 2);
sat.resize(n);
}
// [false] [true]
void add_edge(int i, bool f, int j, bool g){
scc.add_edge(i * 2 + (f ? 0 : 1), j * 2 + (g ? 1 : 0));
scc.add_edge(j * 2 + (g ? 0 : 1), i * 2 + (f ? 1 : 0));
}
// sat[v] = true -> v = false
// sat[v] = false -> v = true
bool satisfiable(){
scc.build();
for(int i = 0; i < n; ++i){
if(scc[i * 2] == scc[i * 2 + 1]) return false;
sat[i] = scc[i * 2] < scc[i * 2 + 1];
}
return true;
}
vector<int> get_sat() { return sat; }
};
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M;
cin >> N >> M;
TwoSAT ts(N * 30);
for(int i = 0; i < M; ++i){
int A, B, Y;
cin >> A >> B >> Y;
--A, --B;
for(int k = 0; k < 30; ++k){
if(Y >> k & 1){
ts.add_edge(A, false, B, false);
ts.add_edge(A, true, B, true);
}
else{
ts.add_edge(A, false, B, true);
ts.add_edge(A, true, B, false);
}
A += N, B += N;
}
}
if(!ts.satisfiable()){
cout << -1 << endl;
return 0;
}
vector<int> ans(N);
for(int i = 0; i < N * 30; ++i){
if(!ts.sat[i]) ans[i % N] += 1 << (i / N);
}
for(int i = 0; i < N; ++i) cout << ans[i] << endl;
return 0;
}