题意:给定n和k,询问n是否等于k个平方数的和,并输出一组解。
第一眼有点唯一分解定理的味道。天梯好像写过这题,当时贼暴力for循环一直除2除2。
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 typedef pair<int,int> piir; 5 const int maxn = 1e5+5; 6 int n,k; 7 int p[maxn]; 8 9 void work() { 10 int tot = 0, bit = 0; 11 int m = n; 12 while (m) { 13 if (m & 1) tot++, p[bit] = 1; 14 m >>= 1; 15 bit++; 16 } 17 18 if (tot <= k) { 19 cout << "YES" << endl; 20 for (int i = bit - 1; i > 0; i--) { 21 if (tot == k) break; 22 while (p[i]) { 23 p[i]--; 24 p[i - 1] += 2; 25 tot++; 26 if (tot == k) break; 27 } 28 } 29 30 int res = 1; 31 for (int i = 0; i < bit; i++) { 32 while (p[i]--) { 33 cout << res << " "; 34 } 35 res *= 2; 36 } 37 } else cout << "NO" << endl; 38 } 39 int main(){ 40 cin>>n>>k; 41 42 if(n<k) cout<<"NO"<<endl; 43 else work(); 44 return 0; 45 }
题意:一个环有n个点顺时针顺序,给出每个点后相邻两个点的序号(顺序不确定),求出该环的原本的顺序(起点无所谓)。
细节没想明白啊!
判断dfs起始方向时,我想的是直接判最后一对u和v,肯定只有一个是和n直接相连的,所以方向就是n->(u|v)。然而在判断时却考虑不详细。
因为u、v只有一个与n相连那么,就是G[n][0|1]去判断!!!
还有一点小优化,因为dfs是先跑G[u][0]这条边的,所以我让与n相连的那条边变成第一条边,这样就直接真正的一边dfs就行了。
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> piir; const int maxn = 2e5+5; int n,k; vector<int> G[maxn]; int vis[maxn]; void dfs(int u){ vis[u]=1;printf(" %d",u); for(int v : G[u]){ if(!vis[v]) dfs(v); } } int main() { scanf("%d", &n); int u, v; for (int i = 0; i < n; i++) { scanf("%d%d", &u, &v); G[u].push_back(v); G[v].push_back(u); } vis[n] = 1; printf("%d", n); if (G[n][0]==u || G[n][1]==u) dfs(u); else dfs(v); return 0; } //if (G[n][1]==u || G[n][1]==v) swap(G[n][0],G[n][1]); // dfs(n);
E.括号匹配
F.使图联通
原文:https://www.cnblogs.com/ordinarv/p/11838983.html