namespace IO { template <typename T> inlinevoidread(T &x) { x = 0; int f = 1; char c = getchar(); while(!isdigit(c)) {if(c == '-') f = -1; c = getchar();} while(isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f; return; }
int n, m, a[N], b[N], fa[N]; vector <int> g[N]; vector <int> G[N]; int dfn[N], low[N], tim; bool vis[N]; int stk[N], top, cnt, id[N]; int val[N], deg[N], w[N], v[N];
namespace IO { template <typename T> inlinevoidread(T &x) { x = 0; int f = 1; char c = getchar(); while(!isdigit(c)) {if(c == '-') f = -1; c = getchar();} while(isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f; return; }
int n, m, k; structedge { int v, w, nxt; } e[M]; int head[N], tot;
voidlink(int u, int v, int w) { e[++tot] = (edge) {v, w, head[u]}; head[u] = tot; }
queue <int> que; int dis[N]; bool vis[N];
voidspfa() { memset(dis, 0x3f, sizeof(dis)); dis[1] = 0; que.push(1), vis[1] = 1; while(!que.empty()) { int u = que.front(); que.pop(); vis[u] = 0; for(int i = head[u]; i; i = e[i].nxt) { int v = e[i].v; if(dis[v] > dis[u] + e[i].w) { dis[v] = dis[u] + e[i].w; if(!vis[v]) que.push(v), vis[v] = 1; } } } return; }
intmain() { read(n), read(m), read(k); for(int i = 1, u, v, w; i <= m; i++) { read(u), read(v), read(w); for(int j = 0; j <= k + 1; j++) { int x = u + j * n, y = v + j * n; link(x, y, w), link(y, x, w); link(x, y + n, w >> 1), link(y, x + n, w >> 1); } }
spfa();
int ans = 1e9; for(int i = 1; i <= k + 1; i++) ans = min(ans, dis[i * n]); write(ans), putchar('\n');
intphi(int x) { int res = x; for(int i = 2; i * i <= x; i++) { if(x % i == 0) { res = res / i * (i - 1); while(x % i == 0) x /= i; } } if(x > 1) res = res / x * (x - 1); return res; }
signedmain() { int n, ans = 0; scanf("%lld", &n); for(int i = 1; i * i <= n; i++) { if(n % i == 0) { ans += i * phi(n / i); if(n / i != i) ans += n / i * phi(i); } } printf("%lld\n", ans); return0; } // A.S.
namespace IO { template <typename T> inlinevoidread(T &x) { x = 0; int f = 1; char c = getchar(); while(!isdigit(c)) {if(c == '-') f = -1; c = getchar();} while(isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f; return; }
constint N = 110; constint M = 1010; constint p = 31011;
int n, m, cnt; structedge { int u, v, w; friendbooloperator < (edge x, edge y) { return x.w < y.w; } } e[M];
structnode { int l, r, num; } a[M];
int f[N];
voidinit() { for(int i = 1; i <= n; i++) f[i] = i; }
intFind(int a) { return f[a] == a ? a : Find(f[a]); }
int sum; voiddfs(int id, int now, int k) { if(now == a[id].r + 1) { if(k == a[id].num) sum++; return; } int x = Find(e[now].u), y = Find(e[now].v); if(x != y) { f[x] = y; dfs(id, now + 1, k + 1); f[x] = x, f[y] = y; } dfs(id, now + 1, k); }
intmain() { read(n), read(m); for(int i = 1; i <= m; i++) read(e[i].u), read(e[i].v), read(e[i].w); sort(e + 1, e + 1 + m);
init(); int tot = 0; for(int i = 1; i <= m; i++) { if(e[i].w != e[i - 1].w) cnt++, a[cnt - 1].r = i - 1, a[cnt].l = i; int x = Find(e[i].u), y = Find(e[i].v); if(x != y) f[x] = y, a[cnt].num++, tot++; } a[cnt].r = m; if(tot != n - 1) { puts("0"); return0; }
init(); int ans = 1; for(int i = 1; i <= cnt; i++) { sum = 0; dfs(i, a[i].l, 0); ans = ans * sum % p; for(int j = a[i].l; j <= a[i].r; j++) { int x = Find(e[j].u), y = Find(e[j].v); if(x != y) f[x] = y; } } write(ans), putchar('\n'); return0; } // A.S.
namespace IO { template <typename T> inlinevoidread(T &x) { x = 0; int f = 1; char c = getchar(); while(!isdigit(c)) {if(c == '-') f = -1; c = getchar();} while(isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f; return; }
#include<bits/stdc++.h> #define ll long long #define pb push_back
usingnamespace std;
namespace IO { template <typename T> inlineboolread(T &x) { x = 0; int f = 1; char c = getchar(); while(!isdigit(c)) {if(c == '-') f = -1; c = getchar();} while(isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f; return x; }
voidClear() { tim = 0, id = 0; ans = 0, tot = 1; memset(dfn, 0, sizeof(dfn)); memset(low, 0, sizeof(low)); memset(vis, 0, sizeof(vis)); memset(cut, 0, sizeof(cut)); for(int i = 1; i <= n; i++) g[i].clear(); n = 0; }
intmain() { while(read(m)) { Clear();
for(int i = 1, u, v; i <= m; i++) { read(u), read(v); g[u].pb(v), g[v].pb(u); n = max(n, max(u, v)); } for(int i = 1; i <= n; i++) if(!dfn[i]) tarjan(i, i);
memset(vis, 0, sizeof(vis)); for(int i = 1; i <= n; i++) { if(vis[i] || cut[i]) continue; id++, num = 0, cnt = 0; dfs(i); if(!cnt) ans += 2, tot *= (ll)num * (num - 1) / 2; elseif(cnt == 1) ans++, tot *= (ll)num; }
namespace IO { template <typename T> inlinevoidread(T &x) { x = 0; int f = 1; char c = getchar(); while(!isdigit(c)) {if(c == '-') f = -1; c = getchar();} while(isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f; return; }
voidcalc() { for(int i = k; i <= n; i++) for(int j = k; j <= m; j++) a[i][j] = max(get_sum(i, j), max(a[i - 1][j], a[i][j - 1])); for(int i = k; i <= n; i++) for(int j = m - k + 1; j >= 1; j--) b[i][j] = max(get_sum(i, j + k - 1), max(b[i - 1][j], b[i][j + 1])); for(int i = n - k + 1; i >= 1; i--) for(int j = k; j <= m; j++) c[i][j] = max(get_sum(i + k - 1, j), max(c[i + 1][j], c[i][j - 1])); for(int i = n - k + 1; i >= 1; i--) for(int j = m - k + 1; j >= 1; j--) d[i][j] = max(get_sum(i + k - 1, j + k - 1), max(d[i + 1][j], d[i][j + 1])); }
intrd() { int x = 0; char c = getchar(); while(!isdigit(c)) c = getchar(), cout << c << endl; while(isdigit(c)) x = x * 10 + c - '0', c = getchar(); return x; }
signedmain() { scanf("%lld%lld%lld", &n, &m, &k); for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) scanf("%lld", &s[i][j]), s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1]; calc(); int ans = 0; for(int i = k; i <= n; i++) for(int j = k; j <= m; j++) { ans = max(ans, b[i - k][1] + get_sum(i, j) + d[i + 1][1]); ans = max(ans, c[1][j - k] + get_sum(i, j) + d[1][j + 1]); } for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { ans = max(ans, a[i][j] + b[i][j + 1] + c[i + 1][m]); ans = max(ans, a[n][j] + b[i][j + 1] + d[i + 1][j + 1]); ans = max(ans, a[i][j] + c[i + 1][j] + b[n][j + 1]); ans = max(ans, a[i][m] + c[i + 1][j] + d[i + 1][j + 1]); } printf("%lld\n", ans); return0; } // A.S.
JSOI2007 建筑抢修
反悔贪心
对 t2 按升序排序,对于第 i 个,如果不能在 t2 内完成,那么前 i 个就最多只能修 i−1 个,贪心的取 t1 最小的就行了,只需要用堆维护一下。
namespace IO { template <typename T> inlinevoidread(T &x) { x = 0; int f = 1; char c = getchar(); while(!isdigit(c)) {if(c == '-') f = -1; c = getchar();} while(isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f; return; }
namespace IO { template <typename T> inlinevoidread(T &x) { x = 0; int f = 1; char c = getchar(); while(!isdigit(c)) {if(c == '-') f = -1; c = getchar();} while(isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f; return; }