最终刷题太多了博客思路慢慢更新
1.利用havel定理判断可不可以简单图化的一道好题
Sample Input 3 7 4 3 1 5 4 2 1 6 4 3 1 4 2 0 6 2 3 1 1 2 1 Sample Output YES 0 1 0 1 1 0 1 1 0 0 1 1 0 0 0 0 0 1 0 0 0 1 1 1 0 1 1 0 1 1 0 1 0 1 0 0 0 0 1 1 0 0 1 0 0 0 0 0 0 NO YES 0 1 0 0 1 0 1 0 0 1 1 0 0 0 0 0 0 1 0 1 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0
#include<iostream> #include<queue> #include<algorithm> #include<cstring> using namespace std; bool MAP[15][15]; typedef pair<int, int> P; std::vector<P> v; bool cmp1(const P& p1, const P& p2) { return p1.second > p2.second; } int main (){ int n; cin >> n; while(n--) { v.clear(); memset(MAP, 0, sizeof(MAP)); int m; cin >> m; for(int i = 1; i <= m; ++i) { int temp; cin >> temp; v.push_back(P(i, temp)); } //sort(v.begin(), v.end(), cmp1); // for(int i = 0; i < m; ++i) // { // cout << v[i].second << " "; // } bool flag = 0; for(int i = 0; i < m; ++i) { sort(v.begin() + i, v.end(), cmp1); for(int j = 0; j < v[i].second; ++j) { if(i + 1 + j < m){ v[i + 1 + j].second--; if(v[i+1+j].second < 0){ flag = 1; break; } MAP[v[i].first][v[i+1+j].first] = 1; MAP[v[i+1+j].first][v[i].first] = 1; } else { flag = 1; break; } } if(flag) { break; } } if(v[m - 1].second == 1) { flag = 1; } if(flag) { cout << "NO" << endl; } else { cout << "YES" << endl; for(int i = 1; i <= m; ++i) { for(int j = 1; j <= m; ++j) { cout << MAP[i][j] << " "; } cout << endl; } } if(n != 0) { cout << endl; } } }
2.
Sample Input 4 1 2 3 4 5 6 1 6 4 1 2 3 4 5 6 7 8 Sample Output 4 2
//并查集裸题 #include <bits/stdc++.h> using namespace std; const int MAXN = 10000005; int ans; int fa[MAXN]; int num[MAXN]; void init() { for(int i = 1; i <= 10000000; ++i) { fa[i] = i; num[i] = 1; } } int find(int x) { return x == fa[x] ? x : (fa[x] = find(fa[x])); } void merge(int i, int j) { int x = find(i); int y = find(j); if(x != y) { fa[x] = y; num[y] += num[x]; //合并十分关键 } } int main () { int m; while(cin >> m) { if(!m){ cout << 1 << endl; continue; } init(); int mm = 0; while(m--) { int A, B; cin >> A >> B; mm = max(max(A,B), mm); merge(A, B); } int temp = 0; for(int i = 1; i <= mm; ++i) { temp = max(num[i], temp); } cout << temp << endl; } }
3.
Sample Input 1 1 1 1 2 1 1 2 Sample Output 1 26
//特别好的一道思维题。并查集加快速幂 #include <bits/stdc++.h> using namespace std; typedef long long ll; const ll MODE = 1000000007; const int MAXN = 1e7 + 5; int fa[MAXN]; void init() { for(int i = 0; i <= 10000000; ++i) { fa[i] = i; } } ll find(ll x) { return x == fa[x] ? x : (fa[x] = find(fa[x])); } int merge(ll i, ll j) { ll x = find(i); ll y = find(j); if(x != y) { fa[x] = y; return 1; } return 0; } ll qpower(ll a, ll b) { ll r = 1; while(b) { if(b & 1) { r = (r * a) % MODE; } a = (a * a) % MODE; b >>= 1; } return r; } int main () { ll N, M; while(cin >> N >> M) { init(); while(M--) { ll L, R; cin >> L >> R; N -= merge(L - 1, R); } cout << qpower(26, N) << endl; } }
4.
//白给题 #include <bits/stdc++.h> using namespace std; multimap<int, int> e; int main () { int n; while(cin >> n && n) { e.clear(); for(int i = 0; i < n; ++i) { int f, t; cin >> f >> t; bool flag = true; for(multimap<int, int>::iterator i = e.find(t); i != e.end() && i -> first == t; ++i) { if(i -> second == f) { e.erase(i); flag = false; break; } } if(flag) { e.insert(make_pair(f, t)); } } if(e.empty()) { cout << "YES" << endl; } else cout << "NO" << endl; } }
5.
#include <bits/stdc++.h> using namespace std; double dp[2][105]; int main () { int n, m, c; dp[0][0] = 1; while(cin >> n >> m >> c && (n + m)) { for(int i = 1; i <= c; ++i) { //dp[0][C]代表总共有C个糖 按如上处理最后是偶数的概率 //dp[1][C]代表总共有C个糖 按如上处理最后是偶数的概率 /* 状态转移方程是 此时最后是偶数的概率等于 原来是奇数的概率乘以这个糖落在n的部分概率即n/m+n 加上 原来是偶数的概率乘以这个糖落在n的部分概率即m/m+n */ dp[0][i] = dp[1][i-1] * (n/(n+m+0.0)) + dp[0][i-1]*(m/(n+m+0.0)); dp[1][i] = dp[0][i-1]*(n/(n+m+0.0)) + dp[1][i-1]*(m/(n+m+0.0)); } cout << setprecision(8) << dp[0][c] << endl; } }
原文:https://www.cnblogs.com/lightac/p/12520934.html