#include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define pdi pair<db,int> #define mp make_pair #define pb push_back #define enter putchar('\n') #define space putchar(' ') #define eps 1e-8 #define mo 974711 #define MAXN 200005 //#define ivorysi using namespace std; typedef long long int64; typedef double db; template<class T> void read(T &res) { res = 0;char c = getchar();T f = 1; while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); } res *= f; } template<class T> void out(T x) { if(x < 0) {x = -x;putchar('-');} if(x >= 10) { out(x / 10); } putchar('0' + x % 10); } int N,M; bool vis[MAXN]; vector<int> to[MAXN]; void Solve() { read(N);read(M); int a,b; vis[N] = 1; for(int i = 1 ; i <= M ; ++i) { read(a);read(b); if(a > b) swap(a,b); if(b == N) vis[a] = 1; to[a].pb(b);to[b].pb(a); } if(vis[1]) {puts("POSSIBLE");return;} for(auto k : to[1]) { if(vis[k]) {puts("POSSIBLE");return;} } puts("IMPOSSIBLE"); } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); return 0; }
因爲發現一個1,2,3,4,5,6,7....N的序列一次操做後能夠變成c++
2,3,4,5,6,7...N,0的序列,這樣N次事後,總會獲得全部數-1的序列spa
也就是,我能夠進行那麼屢次,序列能夠構形成\(K / N\)開始加1到N的序列,就是能夠進行\(\lfloor \frac{K}{N} \rfloor N\)那麼屢次了code
剩下的就從頭開始逆着往回加便可ci
#include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define pdi pair<db,int> #define mp make_pair #define pb push_back #define enter putchar('\n') #define space putchar(' ') #define eps 1e-8 #define mo 974711 #define MAXN 200005 //#define ivorysi using namespace std; typedef long long int64; typedef double db; template<class T> void read(T &res) { res = 0;char c = getchar();T f = 1; while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); } res *= f; } template<class T> void out(T x) { if(x < 0) {x = -x;putchar('-');} if(x >= 10) { out(x / 10); } putchar('0' + x % 10); } int N; int64 a[55],K; void Solve() { read(K); N = 50; a[1] = K / N; for(int i = 2 ; i <= N ; ++i) { a[i] = a[i - 1] + 1; } int t = K % N; for(int i = 1 ; i <= t ; ++i) { a[i] += N; for(int j = 1 ; j <= N ; ++j) { if(i != j) a[j]--; } } out(N);enter; for(int i = 1 ; i <= N ; ++i) { out(a[i]);space; } enter; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); return 0; }
進行一次操做咱們直接把最大的扣到N如下,暴力進行幾回複雜度不會太大get
#include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define pdi pair<db,int> #define mp make_pair #define pb push_back #define enter putchar('\n') #define space putchar(' ') #define eps 1e-8 #define mo 974711 #define MAXN 200005 //#define ivorysi using namespace std; typedef long long int64; typedef double db; template<class T> void read(T &res) { res = 0;char c = getchar();T f = 1; while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); } res *= f; } template<class T> void out(T x) { if(x < 0) {x = -x;putchar('-');} if(x >= 10) { out(x / 10); } putchar('0' + x % 10); } int N; int64 a[55],ans; void Solve() { read(N); for(int i = 1 ; i <= N ; ++i) read(a[i]); while(1) { int t = 1; for(int i = 2 ; i <= N ; ++i) { if(a[i] > a[t]) t = i; } if(a[t] < N) break; int64 k = (a[t] - (N - 1) - 1) / N + 1; a[t] -= k * N; ans += k; for(int i = 1 ; i <= N ; ++i) { if(i != t) a[i] += k; } } out(ans);enter; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); return 0; }
若連通還每一個點就一個入度,那這是一個有向的基環外向樹it
把環上掛着的數給dp完,至關於每次對於兒子取一個未出現過的最小值同樣的操做class
而後環上的點要麼取本身未出現過的最小值,要麼取把這個最小值填上以後下一個沒出現過的test
也就是環上前一個點若是選擇下一個點的第一選項,下一個點必須選擇第二選項queue
把環上每一個點拆成兩個點,按照這種關係連邊,出現大小剛好爲原來環點數的環時合法,若是無環或有一個二倍點數的環就不合法sort
#include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define pdi pair<db,int> #define mp make_pair #define pb push_back #define enter putchar('\n') #define space putchar(' ') #define eps 1e-8 #define mo 974711 #define MAXN 200005 //#define ivorysi using namespace std; typedef long long int64; typedef double db; template<class T> void read(T &res) { res = 0;char c = getchar();T f = 1; while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); } res *= f; } template<class T> void out(T x) { if(x < 0) {x = -x;putchar('-');} if(x >= 10) { out(x / 10); } putchar('0' + x % 10); } int N,p[MAXN],deg[MAXN],val[MAXN]; int id[MAXN][2],tot,cir; vector<int> t[MAXN],to[MAXN * 2]; vector<int> son[MAXN]; queue<int> q; int dfn[MAXN * 2],low[MAXN * 2],sta[MAXN * 2],instack[MAXN * 2],idx,top; bool flag = 0; void Tarjan(int u) { dfn[u] = low[u] = ++idx; sta[++top] = u;instack[u] = 1; for(auto v : to[u]) { if(!dfn[v]) {Tarjan(v);low[u] = min(low[v],low[u]);} else if(instack[u] == 1){ low[u] = min(low[u],dfn[v]); } } if(low[u] == dfn[u]) { int k = 0; while(1) { int x = sta[top--]; ++k; instack[x] = 2; if(x == u) break; } if(k == cir) flag = 1; } } void Solve() { read(N); for(int i = 1 ; i <= N ; ++i) { read(p[i]); deg[p[i]]++; } for(int i = 1 ; i <= N ; ++i) { if(!deg[i]) q.push(i); } while(!q.empty()) { int u = q.front();q.pop(); sort(son[u].begin(),son[u].end()); son[u].erase(unique(son[u].begin(),son[u].end()),son[u].end()); int m = 0; while(m < son[u].size()) { if(son[u][m] != m) break; ++m; } son[p[u]].pb(m); if(!--deg[p[u]]) { q.push(p[u]); } } for(int i = 1 ; i <= N ; ++i) { if(deg[i]) { sort(son[i].begin(),son[i].end()); son[i].erase(unique(son[i].begin(),son[i].end()),son[i].end()); int m = 0,pos = 0; while(1) { if(pos >= son[i].size() || son[i][pos] != m) { t[i].pb(m); if(t[i].size() < 2) {++m;} else break; } else {pos++;++m;} } id[i][0] = ++tot;id[i][1] = ++tot; ++cir; } } for(int i = 1 ; i <= N ; ++i) { if(deg[i]) { int f = p[i]; to[id[f][0]].pb(id[i][t[f][0] == t[i][0]]); to[id[f][1]].pb(id[i][t[f][1] == t[i][0]]); } } for(int i = 1 ; i <= tot ; ++i) { if(!dfn[i]) Tarjan(i); } if(flag) puts("POSSIBLE"); else puts("IMPOSSIBLE"); } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); return 0; }