結果
| 問題 |
No.2464 To DAG
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-09-08 23:18:30 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 6,632 bytes |
| コンパイル時間 | 4,168 ms |
| コンパイル使用メモリ | 279,964 KB |
| 実行使用メモリ | 295,888 KB |
| 最終ジャッジ日時 | 2024-06-26 16:42:13 |
| 合計ジャッジ時間 | 9,133 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | WA * 2 |
| other | TLE * 1 -- * 38 |
ソースコード
# include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const double pi = acos(-1);
template<class T>constexpr T inf() { return ::std::numeric_limits<T>::max(); }
template<class T>constexpr T hinf() { return inf<T>() / 2; }
template <typename T_char>T_char TL(T_char cX) { return tolower(cX); }
template <typename T_char>T_char TU(T_char cX) { return toupper(cX); }
template<class T> bool chmin(T& a,T b) { if(a > b){a = b; return true;} return false; }
template<class T> bool chmax(T& a,T b) { if(a < b){a = b; return true;} return false; }
template<class T> bool is_sqare(T a) { if(floor(sqrt(a)) * floor(sqrt(a)) == a){ return true; }return false; }
int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }
int d_sum(ll n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }
int d_cnt(ll n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }
ll gcd(ll a, ll b) { if (b == 0)return a; return gcd(b, a%b); };
ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g*b; };
template<class T> using dijk = priority_queue<T, vector<T>, greater<T>>;
# define all(qpqpq) (qpqpq).begin(),(qpqpq).end()
# define UNIQUE(wpwpw) sort(all((wpwpw)));(wpwpw).erase(unique(all((wpwpw))),(wpwpw).end())
# define LOWER(epepe) transform(all((epepe)),(epepe).begin(),TL<char>)
# define UPPER(rprpr) transform(all((rprpr)),(rprpr).begin(),TU<char>)
# define rep(i,upupu) for(ll i = 0, i##_len = (upupu);(i) < (i##_len);(i)++)
# define reps(i,opopo) for(ll i = 1, i##_len = (opopo);(i) <= (i##_len);(i)++)
# define len(x) ((int)(x).size())
# define bit(n) (1LL << (n))
# define pb push_back
#ifdef LOCAL
# include "_debug_print.hpp"
# define debug(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__)
#else
# define debug(...) (static_cast<void>(0))
#endif
struct INIT{
INIT(){
std::ios::sync_with_stdio(false);
std::cin.tie(0);
cout << fixed << setprecision(20);
}
}INIT;
class FindingAllCycles {
private:
struct node {
int prev, next, to;
node(const int _prev, const int _next, const int _to)
: prev(_prev), next(_next), to(_to){}
};
const int V;
vector<vector<node> > G;
vector<stack<int> > block_stack;
stack<int> st;
bool *fix, *blocked, *used;
bool enumerate;
void erase_edge(const int u, const int id){
G[u][G[u][id].next].prev = G[u][id].prev;
G[u][G[u][id].prev].next = G[u][id].next;
}
void scc_dfs(const int u, int& tm, int& cnt,
vector<int>& ord, vector<int>& low, vector<int>& cmp, stack<int>& st){
ord[u] = low[u] = tm++, st.push(u);
for(int id = G[u][0].next; id != 0; id = G[u][id].next){
const int v = G[u][id].to;
if(ord[v] < 0){
scc_dfs(v, tm, cnt, ord, low, cmp, st);
low[u] = min(low[u], low[v]);
}else if(cmp[v] < 0){
low[u] = min(low[u], ord[v]);
}
if(cmp[v] >= 0) erase_edge(u, id);
}
if(ord[u] == low[u]){
while(true){
const int v = st.top();
st.pop(), cmp[v] = cnt;
if(v == u) break;
}
++cnt;
}
}
void scc(){
vector<int> ord(V, -1), low(V), cmp(V, -1);
stack<int> st;
int tm = 0, cnt = 0;
for(int i = 0; i < V; ++i){
if(ord[i] < 0) scc_dfs(i, tm, cnt, ord, low, cmp, st);
}
}
void unblock(const int u){
blocked[u] = false;
while(!block_stack[u].empty()){
const int v = block_stack[u].top();
block_stack[u].pop();
if(blocked[v]) unblock(v);
}
}
bool dfs(const int u, const int s, vector<int>& path, vector<int>& ver_list){
bool flag = false;
path.push_back(u);
blocked[u] = true;
if(!used[u]) used[u] = true, ver_list.push_back(u);
for(int id = G[u][0].next; id != 0; id = G[u][id].next){
const int v = G[u][id].to;
if(fix[v]){
erase_edge(u, id);
continue;
}
if(v == s){
if(enumerate) ans.push_back(path);
++count, flag = true;
}else if(!blocked[v]){
if(dfs(v, s, path, ver_list)) flag = true;
}
}
if(flag) unblock(u);
else{
for(int id = G[u][0].next; id != 0; id = G[u][id].next){
block_stack[G[u][id].to].push(u);
}
}
path.pop_back();
return flag;
}
public:
vector<vector<int>> ans;
int count;
FindingAllCycles(const int node_size)
: V(node_size), G(V, vector<node>{{0, 0, -1}}), block_stack(V),
fix(new bool[V]()), blocked(new bool[V]()), used(new bool[V]()), count(0){}
~FindingAllCycles(){
delete[] fix, delete[] blocked, delete[] used;
}
void add_edge(const int u, const int v){
if(u == v){
ans.push_back({u});
return;
}
G[u][0].prev = G[u].back().next = (int)G[u].size();
G[u].push_back((node){(int)G[u].size() - 1, 0, v});
}
int solve(bool _enumerate=true){
scc();
enumerate = _enumerate;
for(int i = 0; i < V; ++i){
vector<int> path, ver_list;
dfs(i, i, path, ver_list);
fix[i] = true;
for(int j : ver_list){
used[j] = blocked[j] = false, block_stack[j] = stack<int>();
}
}
return count;
}
};
void solve(){
int n, m;
cin >> n >> m;
vector<vector<int>> g(n);
vector<int> u(m), v(m);
FindingAllCycles fc(n);
rep(i, m){
cin >> u[i] >> v[i];
u[i]--, v[i]--;
g[u[i]].push_back(v[i]);
fc.add_edge(u[i], v[i]);
}
fc.solve(true);
set<int> ban_v;
set<pair<int, int>> ban;
rep(i, len(fc.ans)){
debug(fc.ans[i]);
bool flg = false;
for(auto x : fc.ans[i]){
if(ban_v.count(x))flg = true;
}
if(flg)continue;
rep(j, len(fc.ans[i])){
ban.insert({fc.ans[i][j], fc.ans[i][(j + 1) % len(fc.ans)]});
ban_v.insert(fc.ans[i][j]);
}
}
rep(i, m){
if(ban.count({u[i], v[i]}))continue;
cout << u[i] + 1 << " " << v[i] + 1 << endl;
}
}
int main(){
int t = 1;
//cin >> t;
while(t--)solve();
}