Codeforces Round # 628 (Div. 2) solution to a problem

Grass, and almost lost points

A: a combination method of seeking \ (A, B \) , so \ (\ gcd (a, b ) + lcm (a, b) = n \)

Initially wrong question stunned 1min, large grass

a = 1, b = n-1 can

B: you \ (n-\) elements, replication \ (n-\) times, output rises up to the strict length of the sub-sequence.

Deduplication can, you can just take a copy of each pass in time, but demanding rise that go heavy.

C: The \ (max \ {mex (u , v) \} \) is the minimum value.

Mispronounce wa out of the question again

We consider, it is the same chain.

Then up the chain of degree 2, and then we found that if the degree is 3, we open to expansion at this point, 0 and 1 on both sides to put on it.

D: a configuration shortest sequence, and such as \ (U \) , and to the exclusive OR \ (V \)

Special sentence out of a pile like it / dk

signed main() {
  // code begin.
    int u , v;
    in >> u >> v;
    if(u > v) {
        out << -1 << '\n' ;
        return 0 ;
    }
    else {
        if(u == v && ! u && ! v) {
            out << 0 << '\n' ;
            return 0 ;
        }
        else {
            if(u == v) {
                out << 1 << '\n' ;
                out << u << '\n' ;
                return 0 ;
            }
            else {
                int left = v - u ;
                if(left & 1) {
                    out << -1 << '\n' ;
                    return 0 ;
                }
                else {
                    left >>= 1;
                    if(u & left) {
                        out << 3 << '\n' ;
                        out << u << ' ' << left << ' ' << left << '\n' ;
                        return 0 ;
                    }
                    else {
                        out << 2 << '\n' ;
                        out << (u | left) << ' ' << left << '\n';
                        return 0 ;
                    }
                }
            }
        }
    }
    return 0;
  // code end.
}

E: a serial to you, you need to select a subsequence, the title to ensure that each element up to two prime factors, so that the product of this sequence is a perfect square, and the shortest such length.
Connected between the prime factors edge, if only one quality factor, and even to zero.
Then we ask about the smallest ring, it is the record has arrived two point i, then on it.

// powered by c++11
// by Isaunoya
#include <bits/stdc++.h>
#define rep(i, x, y) for (register int i = (x); i <= (y); ++i)
#define Rep(i, x, y) for (register int i = (x); i >= (y); --i)
using namespace std;
using db = double;
using ll = long long;
using uint = unsigned int;
#define Tp template
using pii = pair<int, int>;
#define fir first
#define sec second
Tp<class T> void cmax(T& x, const T& y) {
  if (x < y) x = y;
}
Tp<class T> void cmin(T& x, const T& y) {
  if (x > y) x = y;
}
#define all(v) v.begin(), v.end()
#define sz(v) ((int)v.size())
#define pb emplace_back
Tp<class T> void sort(vector<T>& v) { sort(all(v)); }
Tp<class T> void reverse(vector<T>& v) { reverse(all(v)); }
Tp<class T> void unique(vector<T>& v) { sort(all(v)), v.erase(unique(all(v)), v.end()); }
const int SZ = 1 << 23 | 233;
struct FILEIN {
  char qwq[SZ], *S = qwq, *T = qwq, ch;
#ifdef __WIN64
#define GETC getchar
#else
  char GETC() { return (S == T) && (T = (S = qwq) + fread(qwq, 1, SZ, stdin), S == T) ? EOF : *S++; }
#endif
  FILEIN& operator>>(char& c) {
    while (isspace(c = GETC()))
      ;
    return *this;
  }
  FILEIN& operator>>(string& s) {
    while (isspace(ch = GETC()))
      ;
    s = ch;
    while (!isspace(ch = GETC())) s += ch;
    return *this;
  }
  Tp<class T> void read(T& x) {
    bool sign = 0;
    while ((ch = GETC()) < 48) sign ^= (ch == 45);
    x = (ch ^ 48);
    while ((ch = GETC()) > 47) x = (x << 1) + (x << 3) + (ch ^ 48);
    x = sign ? -x : x;
  }
  FILEIN& operator>>(int& x) { return read(x), *this; }
  FILEIN& operator>>(ll& x) { return read(x), *this; }
} in;
struct FILEOUT {
  const static int LIMIT = 1 << 22;
  char quq[SZ], ST[233];
  int sz, O;
  ~FILEOUT() { flush(); }
  void flush() {
    fwrite(quq, 1, O, stdout);
    fflush(stdout);
    O = 0;
  }
  FILEOUT& operator<<(char c) { return quq[O++] = c, *this; }
  FILEOUT& operator<<(string str) {
    if (O > LIMIT) flush();
    for (char c : str) quq[O++] = c;
    return *this;
  }
  Tp<class T> void write(T x) {
    if (O > LIMIT) flush();
    if (x < 0) {
      quq[O++] = 45;
      x = -x;
    }
    do {
      ST[++sz] = x % 10 ^ 48;
      x /= 10;
    } while (x);
    while (sz) quq[O++] = ST[sz--];
  }
  FILEOUT& operator<<(int x) { return write(x), *this; }
  FILEOUT& operator<<(ll x) { return write(x), *this; }
} out;
//#define int long long

const int maxv = 1e3 ;
const int maxn = 1e6 ;
int isprime[maxv + 1];
vector<int> prime;
vector<int> g[maxn];
int d1[maxn], d2[maxn];
int f1[maxn], f2[maxn];

signed main() {
  // code begin.
  for (int i = 2; i <= maxv; i++) {
    if (!isprime[i]) {
      prime.pb(i);
      isprime[i] = sz(prime);
      for (int j = 2 * i; j <= maxv; j += i) isprime[j] = -1;
    }
  }
  int n;
  in >> n;
  int ans = n + 1;
  for (int i = 0; i < n; i++) {
    int x;
    in >> x;
    vector<int> d;
    for (int p : prime) {
      int cnt = 0;
      while (!(x % p)) x /= p, ++cnt;
      if (cnt & 1) d.pb(isprime[p]);
    }
    if (x == 1) {
      if (d.empty()) {
        out << 1 << '\n';
        return 0;
      } else if (sz(d) == 1) {
        g[d[0]].pb(0), g[0].pb(d[0]);
            }
      else {
        g[d[0]].pb(d[1]), g[d[1]].pb(d[0]);
            }
    } else if (d.empty()) {
      g[0].pb(x), g[x].pb(0);
        }
    else {
      g[d[0]].pb(x), g[x].pb(d[0]);
        }
  }
  for (int i = 0; i < 169; i++) {
    fill(d1, d1 + maxn, -1);
    fill(d2, d2 + maxn, -1);
    sort(g[i].begin() , g[i].end()) ;
    if (unique(g[i].begin() , g[i].end()) != g[i].end()) {
      out << 2 << '\n';
      return 0;
    }
    queue<int> q1, q2, q3;
    for (int v : g[i]) q1.push(v), q2.push(1), q3.push(v);
    while (!q1.empty()) {
      int u = q1.front(), d = q2.front(), f = q3.front();
      q1.pop(), q2.pop(), q3.pop();
      if (d1[u] == -1) {
        d1[u] = d, f1[u] = f;
      } else if (d2[u] == -1 && f1[u] ^ f) {
        d2[u] = d, f2[u] = f;
      } else {
        continue;
            }
      for (int v : g[u])
        if (v ^ i)
          q1.push(v), q2.push(d + 1), q3.push(f);
    }
    for (int v : g[i])
      if (~d2[v]) cmin(ans, d2[v] + 1);
  }
  if (ans > n) out << -1 << '\n';
  else out << ans << '\n';
  return 0;
  // code end.
}

F:

Easily find a spanning tree, and then traverse in the above, if there is an edge adjacent to the two points are spaced sqrt 2 can be a direct output.
Otherwise, we stained 01, the final output of a legitimate program, so it / kk

#include<bits/stdc++.h>
using namespace std ;
int n , m ;
const int maxn = 1e6 + 61 ;
vector < int > g[maxn] ;
int dep[maxn] , col[maxn] , fa[maxn] , vis[maxn] , ans[maxn] ;
int sq ;

void dfs(int u) {
    bool qwq = 1 ;
    for(int v : g[u]) {
        if(! dep[v]) {
            col[v] = col[u] ^ 1 ;
            fa[v] = u ;
            dep[v] = dep[u] + 1 ;
            dfs(v) ;
        }
        else {
            if(dep[u] < dep[v] && vis[v]) {
                qwq = 0 ;
            }
            if(dep[u] - dep[v] + 1 >= sq) {
                cout << 2 << '\n' ;
                cout << dep[u] - dep[v] + 1 << '\n' ;
                int now = u ;
                while(now ^ v) {
                    cout << now << ' ' ;
                    now = fa[now] ;
                }
                cout << now << '\n' ;
                exit(0) ;
            }
        }
    }
    vis[u] = qwq ;
    ans[col[u]] += qwq ;
}


void print(int c) {
    cout << 1 << '\n' ;
    for(int i = 1 ; i <= n ; i ++) 
        if(col[i] == c && sq && vis[i])
            cout << i << ' ' , sq -- ;
    exit(0) ;
}

int main() {
    ios :: sync_with_stdio(false) ;
    cin.tie(nullptr) , cout.tie(nullptr) ;
    cin >> n >> m ;
    sq = sqrt(n) ;
    if(sq * sq != n) ++ sq ;
    while(m --) {
        int u , v ;
        cin >> u >> v ;
        g[u].push_back(v);
        g[v].push_back(u) ;
    }
    dfs(dep[1] = 1) ;
    if(ans[0] >= sq) {
        print(0) ;
    }
    else {
        print(1) ;
    }
    return 0 ;
}

Guess you like

Origin www.cnblogs.com/Isaunoya/p/12505758.html