QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#152276 | #6329. Colorful Graph | aesthetic# | WA | 18ms | 5664kb | C++14 | 4.7kb | 2023-08-27 22:24:51 | 2023-08-27 22:24:51 |
Judging History
answer
#include "bits/stdc++.h"
#define endl '\n'
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
using namespace std;
// #define int long long
#define dbg_loc() cerr << __PRETTY_FUNCTION__ << " : " << __LINE__ << "\n"
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p){
return os << '(' << p.first << ", " << p.second << ')';
}
template<typename T_container,typename T=typename enable_if<!is_same<T_container,string>::value, typename T_container::value_type>::type>
ostream& operator<<(ostream &os, const T_container &v){
os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}';
}
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T){
cerr << ' ' << H;
dbg_out(T...);
}
#define LOCAL
#define LOCAL
#ifdef LOCAL
#define dbg(...) cerr<<"(" << #__VA_ARGS__<<"):" , dbg_out(__VA_ARGS__) , cerr << endl
#else
#define dbg(...)
#endif
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
ll mod = (1000000007LL);
inline ll Mod(ll a, ll b){return (a%b);}
inline ll poww(ll a, ll b){ll res = 1;while (b > 0){if(b & 1) res = (res * a)%mod;a = (a * a)%mod;b >>= 1;}return res;}
ll gcd (ll a, ll b) { while (b) { a %= b,swap(a, b);}return a;}
void read(vector<int> &w, int n){w.resize(n);for(int i = 0; i < n; i++) cin>>w[i];}
void print(vector<int> &w){for(int i =0; i < sz(w); i++){if(i == sz(w) - 1) cout<<w[i]<<"\n";else cout<<w[i]<<" ";}}
///CUIDADO COM MAXN
#define N 100020 // 1E6
struct SCC{
vector< vi> g , comps;
vi val, z, cont, comp;
int Time =0, ncomps= 0 , n;
SCC(vector<vi> g, int n ):n(n) , g(g) , val(n+1,0), comp(n+1, -1) {}
int dfs(int j){
int low = val[j] = ++Time , x ; z.push_back(j);
trav(e, g[j]) if(comp[e] < 0)
low = min(low , val[e] ?: dfs(e));
if(low == val[j]){
do{
x = z.back() ; z.pop_back();
comp[x] = ncomps;
cont.push_back(x);
} while(x != j);
comps.push_back(cont) , cont.clear();
ncomps++;
}
return val[j] = low;
}
void scc(){
val.assign(n+1,0); comp.assign(n+1,-1);
Time = ncomps = 0 ;
rep(i,1,n+1) if(comp[i] < 0 ) dfs(i);
}
vector<vi> DAG(){// sem arestas repetidas
vector<vi> dag(n+1);val.assign(n+1,-1);
for(int i=0;i<ncomps;i++){
for(auto v : comps[i]){
for(auto to : g[v]){
if(val[to]==-1 && comp[to]!=i){
val[to]=1;
dag[i].pb(comp[to]);
}
}
}
for(auto v : comps[i])
for(auto to : g[v])
val[to]=-1;
}
return dag;
}
int repre(int c){
// representante da componente c (c < ncomps):
return comps[c][0];
}
};
int n,m;
vector<vi> grafo;
bool find(int j, vector<vi>& g, vi& btoa, vi& vis) {
if (btoa[j] == -1) return 1;
vis[j] = 1; int di = btoa[j];
for (int e : g[di])
if (!vis[e] && find(e, g, btoa, vis)) {
btoa[e] = di;
return 1;
}
return 0;
}
int dfsMatching(vector<vi>& g, vi& btoa) {
vi vis;
rep(i,0,sz(g)) {
vis.assign(sz(btoa), 0);
for (int j : g[i])
if (find(j, g, btoa, vis)) {
btoa[j] = i;
break;
}
}
return sz(btoa) - (int)count(all(btoa), -1);
}
vector<vi> match;
int32_t main(){
ios::sync_with_stdio(false); cin.tie(0);
cin>>n>>m;
grafo.resize(n+1);
for(int i =1,a,b; i <= m; i++){
cin>>a>>b;
grafo[a].pb(b);
}
SCC T = SCC(grafo,n);
T.scc();
auto dag = T.DAG();
// dbg(dag);
match.resize(2*n+1);
vi grau(n+1,0);
rep(i,0,sz(dag)) for(auto j: dag[i]){
match[i].pb(j+n);
grau[j] ++;
// dbg(i,j);
}
vi btoa(2*n+1, -1), cor(2*n+1,-1);
int qtd = dfsMatching(match, btoa);
vi lista;
int t=0;
// dbg(dag);
for(int i = 0; i < T.ncomps; i++)
if(!grau[i]){
lista.pb(i);
cor[i] = ++t;
}
// rep(i,1,n+1) dbg(i,T.comp[i]);
for(int i = 0; i <= 2*n; i++) if(btoa[i] != -1)btoa[btoa[i]] = i;
// dbg(btoa);
//0 até n
for(int i = 0; i < sz(lista); i++){
auto x = lista[i];
if(cor[x] == -1) cor[x] = ++t;
// dbg(x, cor[x]);
for(auto v: dag[x]){
// dbg(x, v);
grau[v] --;
if(v == btoa[x] or v == btoa[x] - n)
cor[v]=cor[x];
if(!grau[v]){
lista.pb(v);
}
}
}
for(int i = 1; i <= n; i++) cout<<cor[T.comp[i]]<<" \n"[i==n];
// dbg(dag);
// dbg(T.comps);
// rep(i,1,n+1) dbg(i,Tcomp[i]));
// cout<<"oi\n";
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3596kb
input:
5 5 1 4 2 3 1 3 2 5 5 1
output:
2 1 1 2 2
result:
ok AC
Test #2:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
5 7 1 2 2 1 4 3 5 1 5 4 4 1 4 5
output:
1 1 2 1 1
result:
ok AC
Test #3:
score: 0
Accepted
time: 0ms
memory: 3616kb
input:
8 6 6 1 3 4 3 6 2 3 4 1 6 4
output:
1 1 1 1 2 1 3 4
result:
ok AC
Test #4:
score: -100
Wrong Answer
time: 18ms
memory: 5664kb
input:
7000 6999 4365 4296 2980 3141 6820 4995 4781 24 2416 5844 2940 2675 3293 2163 3853 5356 262 6706 1985 1497 5241 3803 353 1624 5838 4708 5452 3019 2029 6161 3849 4219 1095 1453 4268 4567 1184 1857 2911 3977 1662 2751 6353 6496 2002 6628 1407 4623 425 1331 4445 4277 1259 3165 4994 1044 2756 5788 5496 ...
output:
1 1949 2587 2137 3 2 3 2510 3041 2241 1 1 4 1 2211 5 2775 6 3139 1789 2740 2909 3 2253 1939 7 8 6 2424 9 2921 2867 10 11 2524 2524 2327 12 3 13 2203 2954 14 2804 15 3111 16 3 17 18 3483 19 2664 3 1973 2110 1 20 3208 21 2881 3 3 3 2053 22 3 3 2236 23 24 3157 25 3309 26 2902 6 2874 3414 3 3 3 2607 263...
result:
wrong answer Integer 1949 violates the range [1, 1750]