-
[ABC376D] Cycle
找到包含节点 1 的环,直接从节点一出发,BFS,如果第二次遍历到了节点1,直接输出时间即可。
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define DEBUG(x) cerr << #x << '=' << x << endl
#define ll long long
typedef pair <int, int> PII;
typedef unsigned int uint;
typedef unsigned long long ull;
#define i128 __int128
#define fi first
#define se second
mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
#define ClockA clock_t start, end; start = clock()
#define ClockB end = clock(); cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
inline int rd(){int x = 0, f = 1;char ch = getchar();while (ch < '0' || ch > '9'){if (ch == '-')f = -1;ch = getchar();}while (ch >= '0' && ch <= '9')x = (x << 3) + (x << 1) + ch - '0', ch = getchar();return x * f;
}
#define rd rd()void wt(int x){if (x < 0)putchar('-'), x = -x;if (x > 9)wt(x / 10);putchar(x % 10 + '0');return;
}
void wt(int x, char k){wt(x),putchar(k);
}namespace Star_F{const int N = 200005;int n, m;bool vis[N], f;int h[N],e[N],ne[N],idx;void add(int a,int b){e[++idx] = b, ne[idx] = h[a], h[a] = idx;}void bfs(){queue<PII> q;q.push({1, 0});while(!q.empty()){int u = q.front().fi,ti=q.front().se;q.pop();for (int i = h[u]; i;i=ne[i]){int j = e[i];if(j==1){cout << ti + 1 << endl;f = 1;return;}if(!vis[j]){q.push({j,ti + 1});vis[j] = 1;}}}} void Main(){n = rd, m = rd;FOR(i,1,m){int a, b;a = rd, b = rd;add(a, b);}bfs();if(!f) cout << -1;}}signed main(){// freopen(".in","r",stdin);// freopen(".out","w",stdout);ClockA;int T=1;// T=rd;while(T--) Star_F::Main();// ClockB;return 0;
}/*
* ▀▀▀██████▄▄▄ _______________
* ▄▄▄▄▄ █████████▄ / \
* ▀▀▀▀█████▌ ▀▐▄ ▀▐█ | Code has no BUG! |
* ▀▀█████▄▄ ▀██████▄██ | _________________/
* ▀▄▄▄▄▄ ▀▀█▄▀█════█▀ |/
* ▀▀▀▄ ▀▀███ ▀ ▄▄
* ▄███▀▀██▄████████▄ ▄▀▀▀▀▀▀█▌ ______________________________
* ██▀▄▄▄██▀▄███▀ ▀▀████ ▄██ █ \\
* ▄▀▀▀▄██▄▀▀▌████▒▒▒▒▒▒███ ▌▄▄▀▀▀▀█_____________________________ //
* ▌ ▐▀████▐███▒▒▒▒▒▐██▌
* ▀▄▄▄▄▀ ▀▀████▒▒▒▒▄██▀
* ▀▀█████████▀
* ▄▄██▀██████▀█
* ▄██▀ ▀▀▀ █
* ▄█ ▐▌
* ▄▄▄▄█▌ ▀█▄▄▄▄▀▀▄
* ▌ ▐ ▀▀▄▄▄▀
* ▀▀▄▄▀ ██
* \ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀
* \- ▌ Name: Star_F ▀ ▀
* - ▌ (o) ▀
* /- ▌ Go Go Go ! ▀ ▀
* / ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀
*/
-
[ABC348F] Oddly Similar
其实就是个暴力,用 bitset 优化一下就行。
先写出用 bool 数组能过的代码,再把 bool数组 最后一维用 bitset 优化掉。
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define DEBUG(x) cerr << #x << '=' << x << endl
#define ll long long
typedef pair <int, int> PII;
typedef unsigned int uint;
typedef unsigned long long ull;
#define i128 __int128
#define fi first
#define se second
mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
#define ClockA clock_t start, end; start = clock()
#define ClockB end = clock(); cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
inline int rd(){int x = 0, f = 1;char ch = getchar();while (ch < '0' || ch > '9'){if (ch == '-')f = -1;ch = getchar();}while (ch >= '0' && ch <= '9')x = (x << 3) + (x << 1) + ch - '0', ch = getchar();return x * f;
}
#define rd rd()void wt(int x){if (x < 0)putchar('-'), x = -x;if (x > 9)wt(x / 10);putchar(x % 10 + '0');return;
}
void wt(int x, char k){wt(x),putchar(k);
}namespace Star_F{const int N = 2001, M = 1000;int n, m, a[N][N], res;bitset<N> cnt[N][M], f;void Main(){n = rd, m = rd;for (int i = 1; i <= n; ++ i )for (int j = 1; j <= m; ++ j ){a[i][j] = rd;cnt[j][a[i][j]][i] = 1;}for (int i = 1; i <= n; ++ i ) {f.reset();for (int j = 1; j <= m; ++ j ) f ^= cnt[j][a[i][j]];f[i] = 0; res += f.count();}wt(res/2, '\n');}
}signed main(){// freopen(".in","r",stdin);// freopen(".out","w",stdout);ClockA;int T=1;// T=rd;while(T--) Star_F::Main();// ClockB;return 0;
}/*
* ▀▀▀██████▄▄▄ _______________
* ▄▄▄▄▄ █████████▄ / \
* ▀▀▀▀█████▌ ▀▐▄ ▀▐█ | Code has no BUG! |
* ▀▀█████▄▄ ▀██████▄██ | _________________/
* ▀▄▄▄▄▄ ▀▀█▄▀█════█▀ |/
* ▀▀▀▄ ▀▀███ ▀ ▄▄
* ▄███▀▀██▄████████▄ ▄▀▀▀▀▀▀█▌ ______________________________
* ██▀▄▄▄██▀▄███▀ ▀▀████ ▄██ █ \\
* ▄▀▀▀▄██▄▀▀▌████▒▒▒▒▒▒███ ▌▄▄▀▀▀▀█_____________________________ //
* ▌ ▐▀████▐███▒▒▒▒▒▐██▌
* ▀▄▄▄▄▀ ▀▀████▒▒▒▒▄██▀
* ▀▀█████████▀
* ▄▄██▀██████▀█
* ▄██▀ ▀▀▀ █
* ▄█ ▐▌
* ▄▄▄▄█▌ ▀█▄▄▄▄▀▀▄
* ▌ ▐ ▀▀▄▄▄▀
* ▀▀▄▄▀ ██
* \ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀
* \- ▌ Name: Star_F ▀ ▀
* - ▌ (o) ▀
* /- ▌ Go Go Go ! ▀ ▀
* / ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀
*/
-
[ABC349D] Divide Interval
挺简单的一道题,贪心。
每次肯定是能跑多远跑多远,把 \(2^i\) 打表出来,从大到小枚举,再判断能否整除。
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define DEBUG(x) cerr << #x << '=' << x << endl
#define ll long long
typedef pair <int, int> PII;
typedef unsigned int uint;
typedef unsigned long long ull;
#define i128 __int128
#define fi first
#define se second
mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
#define ClockA clock_t start, end; start = clock()
#define ClockB end = clock(); cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;#define int long long
inline int rd(){int x = 0, f = 1;char ch = getchar();while (ch < '0' || ch > '9'){if (ch == '-')f = -1;ch = getchar();}while (ch >= '0' && ch <= '9')x = (x << 3) + (x << 1) + ch - '0', ch = getchar();return x * f;
}
#define rd rd()void wt(int x){if (x < 0)putchar('-'), x = -x;if (x > 9)wt(x / 10);putchar(x % 10 + '0');return;
}
void wt(char x){putchar(x);
}
void wt(int x, char k){wt(x),putchar(k);
}namespace Star_F{const int N = 10000005;ll l,r,h1[N],h2[N],ans,a[N];void Main(){l = rd, r = rd;a[0] = 1;FOR(i, 1, 62) a[i] = a[i - 1] << 1ll;while (l != r){ROF(i,60,0){if (l + a[i] <= r && l % a[i] == 0){h1[++ans] = l;h2[ans] = l + a[i];l += a[i];break;}}}wt(ans, '\n');FOR(i,1,ans) cout<<h1[i]<<" "<<h2[i]<<endl;}}signed main(){// freopen(".in","r",stdin);// freopen(".out","w",stdout);ClockA;int T=1;// T=rd;while(T--) Star_F::Main();// ClockB;return 0;
}/*
* ▀▀▀██████▄▄▄ _______________
* ▄▄▄▄▄ █████████▄ / \
* ▀▀▀▀█████▌ ▀▐▄ ▀▐█ | Code has no BUG! |
* ▀▀█████▄▄ ▀██████▄██ | _________________/
* ▀▄▄▄▄▄ ▀▀█▄▀█════█▀ |/
* ▀▀▀▄ ▀▀███ ▀ ▄▄
* ▄███▀▀██▄████████▄ ▄▀▀▀▀▀▀█▌ ______________________________
* ██▀▄▄▄██▀▄███▀ ▀▀████ ▄██ █ \\
* ▄▀▀▀▄██▄▀▀▌████▒▒▒▒▒▒███ ▌▄▄▀▀▀▀█_____________________________ //
* ▌ ▐▀████▐███▒▒▒▒▒▐██▌
* ▀▄▄▄▄▀ ▀▀████▒▒▒▒▄██▀
* ▀▀█████████▀
* ▄▄██▀██████▀█
* ▄██▀ ▀▀▀ █
* ▄█ ▐▌
* ▄▄▄▄█▌ ▀█▄▄▄▄▀▀▄
* ▌ ▐ ▀▀▄▄▄▀
* ▀▀▄▄▀ ██
* \ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀
* \- ▌ Name: Star_F ▀ ▀
* - ▌ (o) ▀
* /- ▌ Go Go Go ! ▀ ▀
* / ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀
*/
-
[AGC032B] Balanced Neighbors
考虑一个图满足
- 不连通
- 每个点和这个点的临点的和为 \(S\)
那么这个图的补图满足
- 联通
- 每个点的所有的邻点的和为 \(T\)(因为顶点总和是不变的,所以 \(T=总顶点和 - S\),而S \(S\) 也是不变的。
构造不连通,且每个点和这个点的临点的和为 \(S\) 的图很容易 构造,按照 \(n\) 的奇偶分类即可。
点击查看代码
#include <bits/stdc++.h>
using namespace std;#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define DEBUG(x) cerr << #x << '=' << x << endl
#define ll long long
typedef pair<int, int> PII;
typedef unsigned int uint;
typedef unsigned long long ull;
#define i128 __int128
#define fi first
#define se second
mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
#define ClockA clock_t start, end; start = clock()
#define ClockB end = clock(); cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;// 输入函数
inline int rd() {int x = 0, f = 1;char ch = getchar();while (ch < '0' || ch > '9') {if (ch == '-') f = -1;ch = getchar();}while (ch >= '0' && ch <= '9')x = (x << 3) + (x << 1) + ch - '0', ch = getchar();return x * f;
}void wt(int x) {if (x < 0) putchar('-'), x = -x;if (x > 9) wt(x / 10);putchar(x % 10 + '0');
}void wt(char x) {putchar(x);
}void wt(int x, char k) {wt(x), putchar(k);
}namespace Star_F {void Main() {int n = rd(); printf("%d\n", n * (n - 1) / 2 - n / 2);FOR(i, 1, n) {FOR(j, i + 1, n) {if (n & 1) { if (i + j != n) { printf("%d %d\n", i, j); }} else { if (i + j != n + 1) { printf("%d %d\n", i, j); }}}}}
}signed main() {// freopen(".in", "r", stdin);// freopen(".out", "w", stdout);ClockA;int T = 1;// T = rd();while (T--) Star_F::Main();ClockB;return 0;
}/*
* ▀▀▀██████▄▄▄ _______________
* ▄▄▄▄▄ █████████▄ / \
* ▀▀▀▀█████▌ ▀▐▄ ▀▐█ | Code has no BUG! |
* ▀▀█████▄▄ ▀██████▄██ | _________________/
* ▀▄▄▄▄▄ ▀▀█▄▀█════█▀ |/
* ▀▀▀▄ ▀▀███ ▀ ▄▄
* ▄███▀▀██▄████████▄ ▄▀▀▀▀▀▀█▌ ______________________________
* ██▀▄▄▄██▀▄███▀ ▀▀████ ▄██ █ \\
* ▄▀▀▀▄██▄▀▀▌████▒▒▒▒▒▒███ ▌▄▄▀▀▀▀█_____________________________ //
* ▌ ▐▀████▐███▒▒▒▒▒▐██▌
* ▀▄▄▄▄▀ ▀▀████▒▒▒▒▄██▀
* ▀▀█████████▀
* ▄▄██▀██████▀█
* ▄██▀ ▀▀▀ █
* ▄█ ▐▌
* ▄▄▄▄█▌ ▀█▄▄▄▄▀▀▄
* ▌ ▐ ▀▀▄▄▄▀
* ▀▀▄▄▀ ██
* \ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀
* \- ▌ Name: Star_F ▀ ▀
* - ▌ (o) ▀
* /- ▌ Go Go Go ! ▀ ▀
* / ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀
*/