Codeforces #529(Div.3)

A.模拟

B.

C.bishat,唯一分解定理

 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 }
View Code

D.实现

  • 分析可以确定ai.1和ai.2是一定相连的,只是不知道方向。我们可以由此建立一张无向图,就是一个环。
  • 因为每个节点只有两条边,所以我们dfs搜环的时候只要确定起点的方向就确定了整个环的方向。

细节没想明白啊!
判断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);
View Code

E.括号匹配

F.使图联通

猜你喜欢

转载自www.cnblogs.com/ordinarv/p/11838983.html