第六章撸完了,马上acm校队就要选拔了,争取加点时间把第七章也撸完
代码如下:
Uva10305 给任务排序:
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 100+5;
int c[maxn];//c[u]==0表示从来没有访问过,c[u]==1表示已经访问过,c[u]==-1表示正在访问,递归调用还没有结束
int G[maxn][maxn];
int topo[maxn], t, f, tt;
bool dfs(int u) {
c[u] = -1;
for (int v = 1; v <= t; v++)
if (G[u][v]) {
if (c[v] < 0)
return false;
else if (!c[v])
dfs(v);
}
c[u] = 1;
topo[--tt] = u;
return true;
}
bool toposort() {
tt = t;
memset(c, 0, sizeof(c));
for (int u = 1; u <= t; u++) {
if (!c[u])
if (!dfs(u))//dfs成功完成
return false;
}
return true;
}
int main() {
while (scanf("%d%d", &t, &f) == 2 && t) {
memset(G, 0, sizeof(G));
memset(topo, 0, sizeof(topo));
int u, v;
for (int i = 0; i < f; i++) {
scanf("%d%d", &u, &v);
G[u][v] = 1;
}
if (toposort()) {
printf("%d", topo[0]);
for (int i = 1; i < t; i++) {
printf(" %d", topo[i]);
}
printf("
");
}
}
return 0;
}
Uva10562看图写树
#include<stdio.h>
#include<iostream>
#include<cctype>
#include<cstring>
using namespace std;
const int maxn = 100 + 5;
int n;
char buf[maxn][maxn];
void dfs(int r, int c) {
printf("%c(", buf[r][c]);
if (buf[r + 1][c] == '|'&&r + 1 < n) {
int i = c;
while (i - 1 >= 0 && buf[r + 2][i - 1] == '-')
i--;
while (buf[r + 2][i] == '-'&&buf[r + 3][i] != ' ') {
if (!isspace(buf[r + 3][i]))
dfs(r + 3, i);
i++;
}
}
printf(")");
}
void solve() {
n = 0;
while (1) {
fgets(buf[n], maxn, stdin);
if (buf[n][0] == '#') break;
else n++;
}
printf("(");
if (n == 0) {
printf(")
");
return;
}
for (int i = 0; i < strlen(buf[0]); i++) {
if (buf[0][i] != ' ') {
dfs(0, i);
break;
}
}
printf(")
");
}
int main() {
int t;
scanf("%d", &t);
getchar();
while (t--)
solve();
return 0;
}
Uva12171 雕塑
#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 50 + 5;
const int maxc = 1000 + 1;
//original data
//discreate data
int nx, ny, nz;
int xs[maxn * 2], ys[maxn * 2], zs[maxn * 2];
//flood related
const int dx[]{ 1,-1,0,0,0,0 };
const int dy[]{ 0,0,1,-1,0,0 };
const int dz[]{ 0,0,0,0,1,-1 };
int color[maxn * 2][maxn * 2][maxn * 2];
struct cell {
int x, y, z;
cell(int x=0,int y=0,int z=0):x(x),y(y),z(z){}
bool valid() const {
return x >= 0 && x < nx - 1 && y >= 0 && y < ny - 1 && z >= 0 && z < nz - 1;
}
bool solid() const {
return color[x][y][z] == 1;
}
bool getvis() const {
return color[x][y][z] == 2;
}
void setvis() const {
color[x][y][z] = 2;
}
cell neighbor(int dir) const {
return cell(x + dx[dir], y + dy[dir], z + dz[dir]);
}
int volume() const {
return (xs[x + 1] - xs[x])*(ys[y + 1] - ys[y])*(zs[z + 1] - zs[z]);
}
int area(int dir) const {
if (dx[dir] != 0) return (ys[y + 1] - ys[y])*(zs[z + 1] - zs[z]);
else if (dy[dir] != 0) return (xs[x + 1] - xs[x])*(zs[z + 1] - zs[z]);
return (xs[x + 1] - xs[x])*(ys[y + 1] - ys[y]);
}
};
void discreate(int* x,int &n) {
sort(x, x + n);
n = unique(x, x + n) - x;
}
int ID(int* x, int n, int x0) {
return lower_bound(x, x + n, x0) - x;
}
void floodfill(int &v, int &s) {
v = 0; s = 0;
cell c;
c.setvis();
queue<cell>q;
q.push(c);
while (!q.empty()) {
cell mc = q.front(); q.pop();
v += mc.volume();
for (int i = 0; i < 6; i++) {
cell cc = mc.neighbor(i);
if (!cc.valid())
continue;
if (cc.solid())
s += cc.area(i);
else if (!cc.getvis()) {
cc.setvis();
q.push(cc);
}
}
}
v = maxc*maxc*maxc - v;
}
int main() {
int T;
scanf("%d", &T);
int n, x0[maxn], y0[maxn], z0[maxn], x1[maxn], y1[maxn], z1[maxn];
while (T--) {
nx = ny = nz = 2;
xs[0] = ys[0] = zs[0] = 0;
xs[1] = ys[1] = zs[1] = maxc;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d%d%d%d%d%d", &x0[i], &y0[i], &z0[i], &x1[i], &y1[i], &z1[i]);
x1[i] += x0[i]; y1[i] += y0[i]; z1[i] += z0[i];
xs[nx++] = x0[i]; xs[nx++] = x1[i];
ys[ny++] = y0[i]; ys[ny++] = y1[i];
zs[nz++] = z0[i]; zs[nz++] = z1[i];
}
discreate(xs, nx);
discreate(ys, ny);
discreate(zs, nz);
memset(color, 0, sizeof(color));
for (int i = 0; i < n; i++) {
int X1 = ID(xs, nx, x0[i]); int X2 = ID(xs, nx, x1[i]);
int Y1 = ID(ys, ny, y0[i]); int Y2 = ID(ys, ny, y1[i]);
int Z1 = ID(zs, nz, z0[i]); int Z2 = ID(zs, nz, z1[i]);
for (int X = X1; X < X2; X++)
for (int Y = Y1; Y < Y2; Y++)
for (int Z = Z1; Z < Z2; Z++)
color[X][Y][Z] = 1;
}
int v = 0, s = 0;
floodfill(v, s);
printf("%d %d
", s, v);
}
return 0;
}
Uva1572,自对应
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int G[52][52];
int c[52];
int ID(char a1, char a2) {
return (a1 - 'A')*2 + (a2 == '+' ? 0 : 1);
}
void connect(char a1, char a2, char b1, char b2) {
if (a1 == '0' || b1 == '0') return;
int u = ID(a1, a2) ^ 1, v = ID(b1, b2);
G[u][v] = 1;
}
bool dfs(int u) {
c[u] = -1;
for (int v = 0; v < 52; v++) if(G[u][v]){
if (c[v] < 0) return true;
else if (c[v]==0 && dfs(v)) return true;
}
c[u] = 1;
return false;
}
bool topo_sort() {
memset(c, 0, sizeof(c));
for (int u = 0; u < 52; u++) if (!c[u])
if (dfs(u)) return true;
return false;
}
int main() {
int n;
while (scanf("%d", &n) == 1 && n) {
memset(G, 0, sizeof(G));
while (n--) {
char s[10];
scanf("%s", s);
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if (i != j)
connect(s[i * 2], s[i * 2 + 1], s[j * 2], s[j * 2 + 1]);
}
if (topo_sort()) printf("unbounded
");
else printf("bounded
");
}
return 0;
}
Uva1599 理想路径
#include<cstdio>
#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn = 1000000 + 5;
const int INF = 1000000000;
struct Edge {
int u, v, c;
Edge(int u = 0, int v = 0, int c = 0) :u(u), v(v), c(c) {}
};
vector<Edge>edges;
vector<int>G[maxn];
int n, vis[maxn], d[maxn];
void add_Edge(int u, int v, int c) {
edges.push_back(Edge(u, v, c));
int idx = edges.size() - 1;
G[u].push_back(idx);
}
void rev_bfs() {
memset(vis, 0, sizeof(vis));
//memset(d, 0, sizeof(d));
queue<int>q;
d[n - 1] = 0; vis[n - 1] = 1;
q.push(n-1);
while (!q.empty()) {
int v = q.front(); q.pop();
for (int i = 0; i < G[v].size(); i++) {
int e = G[v][i];
int u = edges[e].v;
if (!vis[u]) {
d[u] = d[v] + 1;
vis[u] = 1;
q.push(u);
}
}
}
}
void bfs() {
vector<int>ans;
vector<int>next;
memset(vis, 0, sizeof(vis));
vis[0] = 1;
next.push_back(0);
for (int i = 0; i < d[0]; i++) {
int min_color = INF;
for (int j = 0; j < next.size(); j++) {
int u = next[j];
for (int k = 0; k < G[u].size(); k++) {
int e = G[u][k];
int v = edges[e].v;
if (d[u] == d[v] + 1)
min_color = min(min_color, edges[e].c);
}
}
ans.push_back(min_color);
vector<int>next2;
for (int j = 0; j < next.size(); j++) {
int u = next[j];
for (int k = 0; k < G[u].size(); k++) {
int e = G[u][k];
int v = edges[e].v;
if (d[u] == d[v] + 1 && !vis[v] && edges[e].c == min_color) {
vis[v] = 1;
next2.push_back(v);
}
}
}
next = next2;
}
printf("%d
", ans.size());
printf("%d", ans[0]);
for (int i = 1; i < ans.size(); i++)
printf(" %d", ans[i]);
printf("
");
}
int main() {
int u, v, c, m;
while (scanf("%d%d", &n, &m) == 2) {
edges.size();
for (int i = 0; i < n; i++) {
G[i].clear();
}
while (m--) {
scanf("%d%d%d", &u, &v, &c);
add_Edge(u-1, v-1, c);
add_Edge(v-1, u-1, c);
}
rev_bfs();
bfs();
}
return 0;
}
Uva673括号匹配
#include<cstdio>
#include<iostream>
#include<stack>
#include<string>
using namespace std;
int main() {
int n;
scanf("%d", &n);
getchar();
while (n--) {
string s;
getline(cin, s);
if (s.size() == 0) {
cout << "Yes"<<endl;
continue;
}
stack<char>a;
bool flag = true;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '(' || s[i] == '[')
a.push(s[i]);
else if (s[i] == ')')
if (a.size() == 0||a.top() != '(' )
{
flag = false;
break;
}
else
a.pop();
else if (s[i] == ']')
if (a.size() == 0 || a.top() != '[')
{
flag = false;
break;
}
else
a.pop();
}
if (a.size() != 0)
flag = false;
if (flag)
cout << "Yes"<<endl;
else
cout << "No"<<endl;
}
return 0;
}
Uva712 s树
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
char s[8 * 2 + 5], leaf[1 << 8 + 5], vva[8 + 2];
int str[8 + 2];
int main() {
int n, m, kase = 0;
while (scanf("%d", &n) == 1 && n) {
memset(s, 0, sizeof(s));
memset(str, 0, sizeof(str));
for (int i = 0; i < n; i++) {
scanf("%s", s);
str[i] = s[1] - '0';
}
scanf("%s", leaf);
scanf("%d", &m);
vector<char>p;
while (m--) {
scanf("%s", vva);
int len = strlen(vva);
double left = 0, right = strlen(leaf) - 1;
for (int i = 0; i < n; i++) {
if (vva[str[i] - 1] == '0')
right = floor((right + left) / 2);
else
left = ceil((right + left) / 2);
}
p.push_back(leaf[(int)left]);
}
printf("S-Tree #%d:
", ++kase);
for (int i = 0; i < p.size(); i++)
printf("%c", p[i]);
printf("
");
}
return 0;
}
Uva536 树重建
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
char pre1[100000], mid1[100000];
void find(string pre,string mid) {
if (mid.size() == 1) {
printf("%c", mid[0]);
return;
}
else if (mid.size() == 0)
return;
int pos = mid.find(pre[0]);
find(pre.substr(1, pos), mid.substr(0, pos));
find(pre.substr(1 + pos), mid.substr(pos + 1));
printf("%c", pre[0]);
return;
}
int main() {
while (scanf("%s%s", pre1, mid1) == 2) {
string pre, mid;
pre = pre1; mid = mid1;
find(pre, mid);
printf("
");
}
return 0;
}
Uva439 骑士的移动
#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
#include<utility>
using namespace std;
typedef pair<int, int> P;
P str, _end;
int vis[10][10], d[10][10];
int dr[] = { 1,1,2,2,-1,-1,-2,-2 };
int dc[] = { 2,-2,1,-1,2,-2,1,-1 };
bool legal(int nx, int ny) {
return nx > 0 && nx <= 8 && ny > 0 && ny <= 8;
}
int bfs() {
memset(vis, 0, sizeof(vis));
memset(d, 0, sizeof(d));
queue<P>q;
q.push(str);
vis[str.first][str.second] = 1;
d[str.first][str.second] = 0;
while (!q.empty()) {
P p = q.front(); q.pop();
if (p.first == _end.first && p.second == _end.second)
return d[p.first][p.second];
for (int i = 0; i < 8; i++) {
int nx = p.first + dr[i];
int ny = p.second + dc[i];
if (vis[nx][ny] == 0 && legal(nx, ny)) {
vis[nx][ny] = 1;
d[nx][ny] = d[p.first][p.second] + 1;
q.push(make_pair(nx, ny));
}
}
}
}
int main() {
char x0, y0, x1, y1;
while (scanf("%c%c %c%c", &x0, &y0, &x1, &y1) == 4) {
str.first = x0 - 'a' + 1; _end.first = x1 - 'a' + 1;
str.second = 9 - (y0-'0'); _end.second = 9 - (y1-'0');
printf("To get from %c%c to %c%c takes %d knight moves.
", x0, y0, x1, y1, bfs());
getchar();
}
return 0;
}
Uva1600 巡逻机器人
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 20 + 5;
int kase, m, n, k, vis[maxn][maxn][maxn], G[maxn][maxn];
int dr[] = { -1,0,0,1 };
int dc[] = { 0,1,-1,0 };
struct point {
int x, y, dis, blood;
point() :x(0), y(0), dis(0), blood(0) {};
point(int nx,int ny,int ndis, int nblood) :x(nx), y(ny),dis(ndis),blood(nblood) {};
};
bool legal(int nx, int ny) {
return nx >= 0 && nx < m&&ny >= 0 && ny < n;
}
int BFS() {
memset(vis, 0, sizeof(vis));
queue<point>q;
point ini(0, 0, 0, 0);
q.push(ini);
vis[ini.x][ini.y][ini.blood] = 1;
while (!q.empty()) {
point p = q.front(); q.pop();
if (p.x == m - 1 && p.y == n - 1)
return p.dis;
for (int i = 0; i < 4; i++) {
int nx = p.x + dr[i];
int ny = p.y + dc[i];
int nblood = G[nx][ny] ? p.blood + 1 : 0;
if (!vis[nx][ny][nblood] && nblood <= k && legal(nx, ny)) {
point temp(nx, ny, p.dis + 1, nblood);
vis[nx][ny][nblood] = 1;
q.push(temp);
}
}
}
return -1;
}
int main() {
scanf("%d", &kase);
while (kase--) {
scanf("%d%d", &m, &n);
scanf("%d", &k);
memset(G, 0, sizeof(G));
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
scanf("%d", &G[i][j]);
printf("%d
", BFS());
}
return 0;
}
Uva12166 修改天平
#include<cstdio>
#include<iostream>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
typedef long long LL;
string s;
map<LL, int>str;
int main() {
int n;
scanf("%d", &n);
getchar();
while (n--) {
LL a;
int nodeNum = 0, depth = 0;
str.clear();
getline(cin, s);
for (int i = 0; i < s.size(); i++) {
if (s[i] == '[') depth++;
if (s[i] == ']') depth--;
if (s[i] == ',') continue;
if (isdigit(s[i])) {
int j = i + 1;
while (1) {
if (isdigit(s[j])) {
j++;
continue;
}
a = stoll(s.substr(i, j - i));
i = j - 1;
break;
}
a <<= depth;
str[a] += 1;
nodeNum++;
}
}
int k = -1;
for (auto& p : str)
k = max(k, p.second);
printf("%d
", nodeNum - k);
}
return 0;
}
Uva804 Petri网模拟(udebug过)
#include<cstdio>
#include<iostream>
#include<cstring>
#include<map>
using namespace std;
const int maxn = 100 + 5;
struct Node {
map<int, int>in;
map<int, int>out;
};
int NP, NT;
int tokenNum[maxn];
int main() {
int kase = 0;
int time;
while (scanf("%d", &NP) == 1 && NP) {
Node str[maxn];
memset(tokenNum, 0, sizeof(tokenNum));
for (int i = 1; i <= NP; i++)
scanf("%d", &tokenNum[i]);
getchar();
scanf("%d", &NT);
for (int i = 0; i < NT; i++) {
int tmp;
while (scanf("%d", &tmp) == 1 && tmp != 0)
if (tmp < 0)
str[i].out[0 - tmp] += 1;
else
str[i].in[tmp] += 1;
}
scanf("%d", &time);
int i, j;
map<int, int>::iterator it, itt;
for (i = 0; i < time; i++) {
for (j = 0; j < NT; j++) {
int sum = 0;
for (it = str[j].out.begin(); it != str[j].out.end(); advance(it, 1))
if (tokenNum[it->first] < it->second)
break;
if (it == str[j].out.end()) {
for (it = str[j].out.begin(); it != str[j].out.end(); advance(it, 1))
tokenNum[it->first] -= it->second;
for (it = str[j].in.begin(); it != str[j].in.end(); advance(it, 1))
tokenNum[it->first] += it->second;
break;
}
}
if (j == NT)
break;
}
if (i == time)
printf("Case %d: still live after %d transitions
", ++kase, i);
else
printf("Case %d: dead after %d transitions
", ++kase, i);
printf("Places with tokens:");
for (int i = 0; i <= NT; i++)
if (tokenNum[i] > 0)
printf(" %d (%d)", i , tokenNum[i]);
printf("
");
}
return 0;
}
Uva127 纸牌游戏(2920ms过。。。)
#include<cstdio>
#include<iostream>
#include<string>
#include<list>
#include<utility>
#include<stack>
using namespace std;
typedef pair<char, char> P;
typedef list<stack<P>> Type;
list<stack<P>>str;
void ini(string s) {
for (int i = 0; i < s.size(); i += 3) {
stack<P>tmp;
tmp.push(make_pair(s[i], s[i + 1]));
str.push_back(tmp);
}
}
bool equal(stack<P>a, stack<P>b) {
P aa = a.top(), bb = b.top();
return aa.first == bb.first || aa.second == bb.second;
}
void solve() {
Type::iterator it = str.begin(), itt=str.begin();
it++;
while (it != str.end()) {
if (str.size() == 35)
int a = 0;
if (distance(str.begin(),it) >= 3) {
itt = it;
advance(itt, -3);
if (equal(*itt, *it)) {
itt->push(it->top()); it->pop();
if (it->size() == 0)
str.erase(it);
it = str.begin();
it++; continue;
}
}
itt = it;
advance(itt, -1);
if (equal(*itt, *it)) {
itt->push(it->top()); it->pop();
if (it->size() == 0)
str.erase(it);
it = str.begin();
it++; continue;
}
it++;
}
}
void print() {
printf("%d", str.size());
if (str.size() > 1)
printf(" piles remaining:");
else
printf(" pile remaining:");
for (auto i : str)
printf(" %d", i.size());
cout << endl;
}
int main() {
string s;
while (getline(cin, s) && s[0] != '#') {
str.clear(); ini(s);
getline(cin, s); ini(s);
solve();
print();
}
return 0;
}
Uva10410 树重建(未过)
#include<cstdio>
#include<iostream>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
typedef vector<int> vi;
typedef vector<int>::iterator vii;
//FILE *fp = fopen("stdout.txt", "w");
int n;
set<int>str[1000 + 5];
vi find(vi bfs, vi dfs) {
vi tmp;
vii tmp1;
for (int i = 0; i < bfs.size(); i++) {
if (find(dfs.begin(), dfs.end(), bfs[i]) != dfs.end())
tmp.push_back(bfs[i]);
}
return tmp;
}
void solve(vi s_bfs, vi s_dfs) {
//处理两个
if (s_bfs.size() == 2 && s_dfs.size() == 2) {
str[s_bfs[0]].insert(s_bfs[1]);
return;
}
//处理三个
if (s_bfs.size() == 3 && s_dfs.size() == 3) {
if (s_bfs[1] > s_bfs[2]) {
str[s_bfs[0]].insert(s_bfs[1]); str[s_bfs[1]].insert(s_bfs[2]);
return;
}
str[s_bfs[0]].insert(s_bfs[1]); str[s_bfs[0]].insert(s_bfs[2]);
return;
}
vii pos1 = find(s_dfs.begin(), s_dfs.end(), s_bfs[1]);
vii pos2 = find(s_dfs.begin(), s_dfs.end(), s_bfs[2]);
vi dfs1, dfs2, bfs1, bfs2;
//两个孩子
if (s_bfs[1] < s_bfs[2] && pos2 - pos1>1) {
str[s_bfs[0]].insert(s_bfs[1]); str[s_bfs[0]].insert(s_bfs[2]);
dfs1.assign(pos1, pos2); dfs2.assign(pos2, s_dfs.end());
vi mid1(find(s_bfs, dfs1)), mid2 = (find(s_bfs, dfs2));
bfs1.swap(mid1); bfs2.swap(mid2);
solve(bfs1, dfs1);
if (pos2 != s_dfs.end() - 1)
solve(bfs2, dfs2);
return;
}
//一个孩子
if (s_bfs[1] > s_bfs[2] || pos1 == pos2 + 1) {
str[s_bfs[0]].insert(s_bfs[1]);
pos1 = s_bfs.begin() + 1; pos2 = s_dfs.begin() + 1;
bfs1.assign(pos1, s_bfs.end()); dfs1.assign(pos2, s_dfs.end());
solve(bfs1, dfs1);
return;
}
}
void print() {
for (int i = 1; i <= n; i++) {
printf("%d:", i);
//fprintf(fp,"%d:", i);
if (str[i].size() != 0)
for (auto j : str[i])
printf(" %d", j);
//fprintf(fp, " %d", j);
printf("
");
//fprintf(fp,"
");
}
}
int main() {
while (scanf("%d", &n)==1&&n!=0) {
for (int i = 1; i <= n; i++)
str[i].clear();
getchar();
vi s_bfs, s_dfs;
int tmp;
for (int i = 0; i < n; i++) {
scanf("%d", &tmp);
s_bfs.push_back(tmp);
}
for (int i = 0; i < n; i++) {
scanf("%d", &tmp);
s_dfs.push_back(tmp);
}
solve(s_bfs, s_dfs);
print();
}
return 0;
}
Uva10410 树重建(已过)
//给定一个树的bfs和dfs,求每个节点的儿子列表
/*
按bfs的序列来模拟dfs的输入,通过栈来实现,因为bfs映射了每个节点到根的距离,
所以再读入bfs序列后,通过栈的方式来模拟dfs的进程,如果dfs读入的数据的位置
大于站定元素的位置,那说明他就是这个元素的儿子,将其入栈,这因为这是dfs型的
遍历,你每次肯定会在遍历栈顶元素之后遍历它的儿子,当读入元素的位置小于栈顶元
素时,表明栈顶元素已经是叶节点,此时你应该回溯,这时候就是栈顶元素出栈,继续
上述操作,直到dfs序列遍历完毕
*/
#include<cstdio>
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
const int maxn = 1000 + 5;
vector<int>G[maxn];
int pos[maxn], n;
int main() {
while (scanf("%d", &n) == 1) {
int x;
for (int i = 1; i <= n; i++) {
scanf("%d", &x);
G[i].clear();
pos[x] = i;
}
int root;
stack<int>sta;
scanf("%d", &root);
sta.push(root);
for (int i = 1; i < n; i++) {
scanf("%d", &x);
while (true) {
int u = sta.top();
if (u == root || pos[u] + 1 < pos[x]) {
G[u].push_back(x);
sta.push(x);
break;
}
else
sta.pop();
}
}
for (int i = 1; i <= n; i++) {
printf("%d:", i);
for (int j = 0; j < G[i].size(); j++) {
printf(" %d", G[i][j]);
}
printf("
");
}
}
return 0;
}
Uva12118 检察员的难题
/*题意:
V个城市,E个指定道路,每个城市间都有一条长为T的路,不要求起点终点,
求最短走完要求路的长度
*/
/*求解思路:
E条指定道路是必须走的,所以我们只需要关心哪些路是需要额外走的,
dfs求联通块,每个联通块内求这些点是否存在欧拉道路,即判断奇度
数点的个数P(无向图最多存在两个),若P大于2,则需要增加
((P-2)/2)条道路使其构成欧拉道路,再通过求出来的联通快的个数
判断需要在不同联通快间加多少条道路
*/
#include<cstdio>
#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1000 + 5;
int V, E, T, vis[maxn];
vector<int>G[maxn];
int dfs(int u,int cnt) {
if (vis[u]) return 0;
vis[u] = cnt;
int sz = G[u].size();
int oddNum = sz % 2;
for (int j = 0; j < sz; j++)
oddNum += dfs(G[u][j], cnt);
return oddNum;
}
int main() {
int kase = 0;
while (scanf("%d%d%d", &V, &E, &T) == 3 && (V || E || T)) {
for (int i = 0; i < V; i++)
G[i].clear();
memset(vis, 0, sizeof(vis));
int m, n;
for (int i = 0; i < E; i++) {
scanf("%d%d", &m, &n);
G[m - 1].push_back(n - 1);
G[n - 1].push_back(m - 1);
}
int cnt = 0, len = E;
for (int i = 0; i < V; i++) {
if (vis[i] || G[i].empty())
continue;
len += max(0, (dfs(i, ++cnt) - 2) / 2);//运算结果可能为负数,所以用max
}
printf("Case %d: %d
", ++kase, T*(len + max(0, cnt - 1)));
}
return 0;
}
课程作业,乱点鸳鸯谱(更新版44种情况)
#include<cstdio>
#include<iostream>
#include<vector>
using namespace std;
vector<int>compare;//标准情况比较数组
int kase;
void print_permutation(int n, int *ans, int cur){
if (cur == n) {
printf( "The %d situation:
", ++kase);
for (auto i : compare) printf("%d ", i);
printf("
");
for (int i = 0; i < n; i++)
printf("%d", ans[i]);
printf("
");
}
else
for (int i = 1; i <= n; i++) {
int ok = 1;//标志符,为1表示可以填充
if (compare[cur] != i) {
for (int j = 0; j < cur; j++)
//if (ans[j] == i || compare[cur - 1] == i)
if (ans[j] == i)
//前者表示已经用过,后者表示对应位相等
ok = 0;
if (ok) {
ans[cur] = i;
print_permutation(n, ans, cur + 1);
}
}
}
}
int main() {
int ans[6];
for (int i = 1; i <= 5; i++)
compare.push_back(i);
print_permutation(5, ans, 0);
//第一个参数表示要求生成排列的总位数,第二个表示当前位数
return 0;
}
继续加油!