QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#152318#6329. Colorful Graphaesthetic#TL 1ms3820kbC++145.5kb2023-08-28 00:14:222023-08-28 00:14:22

Judging History

你现在查看的是最新测评结果

  • [2023-08-28 00:14:22]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3820kb
  • [2023-08-28 00:14:22]
  • 提交

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 7020 // 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[comp[to]]==-1 && comp[to]!=i){
            val[comp[to]]=1;
            dag[i].pb(comp[to]);
          }
        }
      }
      for(auto v : comps[i])
        for(auto to : g[v])
          val[comp[to]]=-1;
    }
    return dag;
  }
  int repre(int c){
    // representante da componente c (c < ncomps):
    return comps[c][0];
  }
};
int  n,m;
vector<vi> grafo;

vector<bitset<7010>> mat;
bool find(int j, int nn, vi& btoa, vi& vis) {
    if (btoa[j] == -1) return 1;
    vis[j] = 1; int di = btoa[j];
    for (int k = 0; k < nn; ++k)
    {
    	if(k == j || !mat[j][k])
    		continue;
    	int e = k;
        if (!vis[e] && find(e, nn, btoa, vis)) {
            btoa[e] = di;
            return 1;
        }
    }
    return 0;
}
int dfsMatching(int nn, vi& btoa) {
    vi vis;
    rep(i,0,nn) {
        vis.assign(sz(btoa), 0);
        for (int j = 0; j < nn; ++j)
        {
        	if(i == j)
        		continue;
        	if(!mat[i][j])
        		continue;
            if (find(j, nn, btoa, vis)) {
                btoa[j] = i;
                break;
            }
        	
        }
    }
    return sz(btoa) - (int)count(all(btoa), -1);
}

bool porra[N];
void dfs(int x, vector<vi> &dag){
	porra[x] = 1;
	for(auto v: dag[x]){
		mat[x] |= mat[v];
		if(porra[v]) continue;
		dfs(v, dag);
	}
	mat[x].set(x);
}

struct UF {
	vi e;
	UF(int n) : e(n, -1) {}
	bool sameSet(int a, int b) { return find(a) == find(b); }
	int size(int x) { return -e[find(x)]; }
	int find(int x) { return e[x] < 0 ? x : e[x] = find(e[x]); }
	bool join(int a, int b) {
		a = find(a), b = find(b);
		if (a == b) return false;
		if (e[a] > e[b]) swap(a, b);
		e[a] += e[b]; e[b] = a;
		return true;
	}
};
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);
    mat.resize(n+1);
    vi grau(n+1,0);
    
    rep(i,0,T.ncomps){
    	memset(porra,0,sizeof porra);
    	if(!porra[i])dfs(i, dag);
    }

    // return 0;

    UF cu(n+1);

    vi btoa(n + 1, -1), cor(n+1,-1);
    int qtd = dfsMatching(T.ncomps, btoa);

    for(int i = 0; i <= n; i++){
    	if(btoa[i] != -1) cu.join(i,btoa[i]);
    }

    int t=0;
    for(int i = 0; i <= n; i++){
    	int pai = cu.find(i);

    	// dbg(i,pai);
    	if(cor[pai] == -1) cor[pai] = ++t;
    }

    for(int i = 1; i <= n; i++){
    	int cc = cor[  cu.find(T.comp[i]) ];
    	// assert(1 <= cc and cc <= t);
    	cout<<cc<<" \n"[i==n];
    }
    // dbg(dag);
    // dbg(T.comps);
    // rep(i,1,n+1) dbg(i,Tcomp[i]));

    // cout<<"oi\n";
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3764kb

input:

5 5
1 4
2 3
1 3
2 5
5 1

output:

1 1 2 1 2

result:

ok AC

Test #2:

score: 0
Accepted
time: 1ms
memory: 3560kb

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: 1ms
memory: 3820kb

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
Time Limit Exceeded

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:


result: