bzoj 1051 1054 1880 2330 3668
parent
57d610fa2b
commit
d3bc36c938
@ -0,0 +1,63 @@
|
||||
#include <cstdio>
|
||||
inline int min(int a, int b) { return a < b ? a : b; }
|
||||
int head[10010], next[50010], to[50010], ecnt;
|
||||
int dfn[10010], low[10010], stk[10010], scc[10010], top, idx, scccnt;
|
||||
bool instk[10010];
|
||||
int deg[10010];
|
||||
inline void addEdge(int f, int t)
|
||||
{
|
||||
ecnt++;
|
||||
next[ecnt] = head[f];
|
||||
head[f] = ecnt;
|
||||
to[ecnt] = t;
|
||||
}
|
||||
void tarjan(int x)
|
||||
{
|
||||
dfn[x] = low[x] = ++idx;
|
||||
instk[stk[top++] = x] = true;
|
||||
for (int cur = head[x]; cur; cur = next[cur])
|
||||
if (!dfn[to[cur]]) tarjan(to[cur]), low[x] = min(low[x], low[to[cur]]);
|
||||
else if (instk[to[cur]])
|
||||
low[x] = min(low[x], dfn[to[cur]]);
|
||||
if (dfn[x] == low[x])
|
||||
{
|
||||
scccnt++;
|
||||
do
|
||||
{
|
||||
top--;
|
||||
scc[stk[top]] = scccnt;
|
||||
instk[stk[top]] = false;
|
||||
} while (stk[top] != x);
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n, m;
|
||||
scanf("%d%d", &n, &m);
|
||||
for (int i = 0, x, y; i < m; i++)
|
||||
{
|
||||
scanf("%d%d", &x, &y);
|
||||
addEdge(x, y);
|
||||
}
|
||||
for (int i = 1; i <= n; i++)
|
||||
if (!dfn[i])
|
||||
tarjan(i);
|
||||
for (int i = 1; i <= n; i++)
|
||||
for (int cur = head[i]; cur; cur = next[cur])
|
||||
if (scc[i] != scc[to[cur]])
|
||||
deg[scc[i]]++;
|
||||
int zcnt = 0, id = 0;
|
||||
for (int i = 1; i <= scccnt; i++)
|
||||
if (deg[i] == 0)
|
||||
zcnt++, id = i;
|
||||
if (zcnt != 1) putchar('0');
|
||||
else
|
||||
{
|
||||
int ans = 0;
|
||||
for (int i = 1; i <= n; i++)
|
||||
if (scc[i] == id)
|
||||
ans++;
|
||||
printf("%d", ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -0,0 +1,57 @@
|
||||
#include <cstdio>
|
||||
#include <cctype>
|
||||
int s, t, que[1 << 16 | 1], vis[1 << 16 | 1], head, tail;
|
||||
int a[4][4];
|
||||
const int dx[] = { 1, 0, -1, 0 };
|
||||
const int dy[] = { 0, -1, 0, 1 };
|
||||
inline void unzip(int x)
|
||||
{
|
||||
for (int i = 3; i >= 0; i--)
|
||||
for (int j = 3; j >= 0; j--)
|
||||
a[i][j] = x & 1, x >>= 1;
|
||||
}
|
||||
inline int zip()
|
||||
{
|
||||
int ret = 0;
|
||||
for (int i = 0; i < 4; i++)
|
||||
for (int j = 0; j < 4; j++)
|
||||
ret = (ret << 1) | a[i][j];
|
||||
return ret;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
for (int i = 0, ch; i < 16; i++)
|
||||
{
|
||||
while (!isdigit(ch = getchar()));
|
||||
s = (s << 1) | (ch == '1');
|
||||
}
|
||||
for (int i = 0, ch; i < 16; i++)
|
||||
{
|
||||
while (!isdigit(ch = getchar()));
|
||||
t = (t << 1) | (ch == '1');
|
||||
}
|
||||
que[tail++] = s;
|
||||
for (int x, tmp; head ^ tail; head++)
|
||||
{
|
||||
x = que[head];
|
||||
if (x == t) break;
|
||||
unzip(x);
|
||||
for (int i = 0; i < 4; i++)
|
||||
for (int j = 0; j < 4; j++)
|
||||
if (a[i][j]) for (int d = 0; d < 4; d++)
|
||||
{
|
||||
i += dx[d], j += dy[d];
|
||||
if (i >= 0 && i <= 3 && j >= 0 && j <= 3 && a[i][j] == 0)
|
||||
{
|
||||
a[i - dx[d]][j - dy[d]] = 0, a[i][j] = 1;
|
||||
tmp = zip();
|
||||
if (!vis[tmp]) vis[que[tail++] = tmp] = vis[x] + 1;
|
||||
a[i - dx[d]][j - dy[d]] = 1, a[i][j] = 0;
|
||||
}
|
||||
i -= dx[d], j -= dy[d];
|
||||
}
|
||||
|
||||
}
|
||||
printf("%d", vis[t]);
|
||||
return 0;
|
||||
}
|
||||
@ -0,0 +1,50 @@
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
inline int abs(int x) { return x >= 0 ? x : -1; }
|
||||
inline int max(int a, int b) { return a > b ? a : b; }
|
||||
int head[1510], next[1000010], to[1000010], len[1000010], ecnt, ans;
|
||||
int disS1[1510], disS2[1510], disE1[1510], disE2[1510], que[1000010];
|
||||
bool inq[1510];
|
||||
inline void addEdge(int f, int t, int l)
|
||||
{
|
||||
ecnt++;
|
||||
next[ecnt] = head[f];
|
||||
head[f] = ecnt;
|
||||
to[ecnt] = t;
|
||||
len[ecnt] = l;
|
||||
}
|
||||
void spfa(int s, int *dis)
|
||||
{
|
||||
memset(inq, 0, sizeof(inq));
|
||||
int h, t, x;
|
||||
h = t = x = 0;
|
||||
for (dis[s] = 0, que[t++] = s, inq[s] = true; h ^ t; inq[que[h++]] = false)
|
||||
for (int cur = head[que[h]]; cur; cur = next[cur])
|
||||
if (dis[to[cur]] > dis[que[h]] + len[cur])
|
||||
{
|
||||
dis[to[cur]] = dis[que[h]] + len[cur];
|
||||
if (!inq[to[cur]]) que[t++] = to[cur], inq[to[cur]] = true;
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
memset(disS1, 0x3f3f3f3f, sizeof(disS1));
|
||||
memset(disS2, 0x3f3f3f3f, sizeof(disS2));
|
||||
memset(disE1, 0x3f3f3f3f, sizeof(disE1));
|
||||
memset(disE2, 0x3f3f3f3f, sizeof(disE2));
|
||||
int n, m, x1, y1, x2, y2;
|
||||
scanf("%d%d%d%d%d%d", &n, &m, &x1, &y1, &x2, &y2);
|
||||
for (int i = 0, x, y, z; i < m; i++)
|
||||
{
|
||||
scanf("%d%d%d", &x, &y, &z);
|
||||
addEdge(x, y, z);
|
||||
addEdge(y, x, z);
|
||||
}
|
||||
spfa(x1, disS1), spfa(x2, disS2), spfa(y1, disE1), spfa(y2, disE2);
|
||||
int len1 = disS1[y1], len2 = disS2[y2];
|
||||
for (int i = 1; i <= n; i++) if (disS1[i] + disE1[i] == len1 && disS2[i] + disE2[i] == len2)
|
||||
for (int j = 1; j <= n; j++) if (disS1[j] + disE1[j] == len1 && disS2[j] + disE2[j] == len2)
|
||||
ans = max(ans, abs(disS1[i] - disS1[j]));
|
||||
printf("%d", ans);
|
||||
return 0;
|
||||
}
|
||||
@ -0,0 +1,47 @@
|
||||
#include <cstdio>
|
||||
int head[100010], next[500010], to[500010], len[500010], ecnt, n, k;
|
||||
int que[10000010], dis[100010], vis[100010];
|
||||
bool inq[100010];
|
||||
inline void addEdge(int f, int t, int l)
|
||||
{
|
||||
ecnt++;
|
||||
next[ecnt] = head[f];
|
||||
head[f] = ecnt;
|
||||
to[ecnt] = t;
|
||||
len[ecnt] = l;
|
||||
}
|
||||
bool spfa()
|
||||
{
|
||||
dis[0] = 0, inq[0] = true, vis[0] = 1;
|
||||
int f, r;
|
||||
f = r = 0;
|
||||
que[r++] = 0;
|
||||
for (int x; f ^ r; inq[que[f++]] = false)
|
||||
for (int cur = head[x = que[f]]; cur; cur = next[cur])
|
||||
if (dis[to[cur]] < dis[x] + len[cur])
|
||||
{
|
||||
dis[to[cur]] = dis[x] + len[cur];
|
||||
if (++vis[to[cur]] > n || to[cur] == x) return false;
|
||||
if (!inq[to[cur]]) inq[que[r++] = to[cur]] = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
scanf("%d%d", &n, &k);
|
||||
for (int i = 0, x, a, b; i < k; i++)
|
||||
{
|
||||
scanf("%d%d%d", &x, &a, &b);
|
||||
if (x == 1) addEdge(a, b, 0), addEdge(b, a, 0);
|
||||
if (x == 2) addEdge(a, b, 1);
|
||||
if (x == 3) addEdge(b, a, 0);
|
||||
if (x == 4) addEdge(b, a, 1);
|
||||
if (x == 5) addEdge(a, b, 0);
|
||||
}
|
||||
for (int i = n; i; i--) addEdge(0, i, 1);
|
||||
long long ans = 0;
|
||||
if (!spfa()) ans = -1;
|
||||
else for (int i = 1; i <= n; i++) ans += dis[i];
|
||||
printf("%lld", ans);
|
||||
return 0;
|
||||
}
|
||||
@ -0,0 +1,29 @@
|
||||
#include <cstdio>
|
||||
int a[100010], n, m;
|
||||
char op[100010], buf[10];
|
||||
int calc(int x)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
if (op[i] == 'A') x &= a[i];
|
||||
if (op[i] == 'O') x |= a[i];
|
||||
if (op[i] == 'X') x ^= a[i];
|
||||
}
|
||||
return x;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
scanf("%d%d", &n, &m);
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
scanf("%s%d", buf, a + i);
|
||||
op[i] = buf[0];
|
||||
}
|
||||
int cur = 1, ans = 0;
|
||||
while (cur <= m) cur <<= 1;
|
||||
for (cur >>= 1; cur; cur >>= 1)
|
||||
if ((!(calc(0) & cur)) && (ans + cur <= m) && (calc(cur) & cur))
|
||||
ans += cur;
|
||||
printf("%d", calc(ans));
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in new issue