本文主要是介绍每日错题(已经红温,气死我了,啊啊啊啊啊啊),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
总共用2^n中可能,但是我tm的,就想着贪心了,woc,真的啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊。
Problem - D - Codeforces
题解地址
思路,对于n个牌子,有相同的牌子必然是不能再一起的。这里想到二分图。用染色法判断二分图。
E. Split Into Two Sets(ranting 1600)超详细题解-CSDN博客
#include <bits/stdc++.h>using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> PII;
#define pb emplace_back
//#define int ll
#define all(a) a.begin(),a.end()
#define x first
#define y second
#define ps push_back
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define lc u << 1
#define rc u << 1 | 1void solve();const int N = 1e6 + 10;signed main() {IOS;ll t = 1;cin >> t;while (t--)solve();return 0;
}struct eage{ll v,ne;}e[N];
ll h[N],idx;
ll color[N];//2种颜色,1和2ll n;
void add(ll u,ll v)
{e[++idx] = {v, h[u]};h[u] = idx;
}bool dfs(ll u,ll c)//节点,颜色
{color[u] = c;//染色for(int i = h[u]; i; i = e[i].ne){ll v = e[i].v;if(!color[v]){if(dfs(v,3-c)) return 1;//有奇环}else if(color[v] == c) return 1;//颜色相同,有奇环}return 0;
}void init()
{for(int i = 0; i <= idx; ++ i){e[i] = {0,0};}idx = 0;for(int i = 1; i <= n; ++ i){h[i] = color[i] = 0;}
}void solve() {init();//多测不初始化,亲人2行泪。wa1,wa2,wa3,,,cin >> n;vector<ll> num(n+1,0);bool ok = 1;vector<vector<ll>> ee(n+1);for(int i=1,u,v; i <= n; ++ i){cin >> u >> v;++ num[u],++ num[v];ee[u].ps(i),ee[v].ps(i);if(u == v) ok = 0;}for(int i = 1; i <= n; ++ i)if(num[i]>2 || ok == 0){cout << "NO" << endl;return;}for(int i = 1; i <= n; ++ i){ll u,v;u = ee[i][0],v = ee[i][1];add(u,v);add(v,u);}for(int i = 1; i <= n; ++ i){if(!color[i]){if(dfs(i,1)){ok = 0;//有奇环break;}}}for(int i = 1; i <= n; ++ i){if(!color[i]){ok = 0; break;}}if(ok) cout << "YES" << endl;else cout << "NO" << endl;
}
这篇关于每日错题(已经红温,气死我了,啊啊啊啊啊啊)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!