Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
- using ll = long long;
- const int INF = 1e9 + 7;
- double rng() { return double(rand()) / RAND_MAX; }
- int n, m;
- vector<set<int>> gr;
- vector<int> color;
- int dfs(int u) {
- int cnt = 0;
- for (int v : gr[u]) {
- if (color[u] == color[v]) cnt++;
- }
- return cnt;
- }
- signed main() {
- cin >> n >> m;
- gr.resize(n);
- for (int i = 0; i < m; i++) {
- int a, b;
- cin >> a >> b;
- --a;
- --b;
- gr[a].insert(b);
- gr[b].insert(a);
- }
- color.resize(n);
- for (int i = 0; i < n; i++) {
- color[i] = rnd() % 3;
- }
- int start = clock();
- long double t = 1.0;
- int f_old = 0;
- for (int i = 0; i < n; i++) {
- f_old += dfs(i);
- }
- while (clock() - start <= 1.9 * CLOCKS_PER_SEC && f_old > 0) {
- if (t <= 0.00001) {
- for (int i = 0; i < n; i++) {
- color[i] = rnd() % 3;
- }
- f_old = 0;
- for (int i = 0; i < n; i++) {
- f_old += dfs(i);
- }
- if (!f_old) break;
- t = 1.0;
- }
- int i = rnd() % n;
- int prev_color = color[i];
- int flip_color = rnd() % 3;
- while (flip_color == prev_color) {
- flip_color = rnd() % 3;
- }
- int cnt = f_old;
- cnt -= dfs(i);
- color[i] = flip_color;
- cnt += dfs(i);
- if (cnt < f_old || rng() < exp(((double) f_old - cnt) / t)) {
- f_old = cnt;
- } else {
- color[i] = prev_color;
- }
- t *= 0.99;
- }
- for (int i : color) {
- cout << i + 1 << ' ';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement