Gym

来源:互联网 发布:rpgmaker python 编辑:程序博客网 时间:2024/06/10 06:54

Problem K  Balls and Needles


Joana Vasconcelos is a Portuguese artist who uses everyday
objects in her creations, like electric irons or plastic cutlery.
She is an inspiration to Ana, who wants to make ceiling hanging
sculptures with straight knitting needles and balls of wool. For
safety reasons, there will be a ball at each end of each needle.
Knitting needles vary in colour, length and thickness (to allow
intersections of needles).
Sculptures are to be exhibited in room corners, which provide
a 3D Cartesian coordinate system, with many lamps on the
ceiling. Sculpture designs are made with the coordinates of the
centres of the balls of wool in which knitting needles are stuck.
That is, each needle N is represented by a set of two dierent
triples: N = {(x; y; z); (x0; y0; z0)}.
Ana dislikes closed chains. A true closed chain is a sequence of
k distinct needles, N1;N2; : : : ;Nk (for some k <= 3), such that:
 ·N1 = {(x1; y1; z1); (x2; y2; z2)}; N2 = {(x2; y2; z2); (x3; y3; z3)}; : : : ;
Nk = {(xk; yk; zk); (xk+1; yk+1; zk+1)}; and (xk+1; yk+1; zk+1) = (x1; y1; z1).
But her dislike of closed chains is so extreme that the shadow of the sculpture on the oor
has to be free of oor closed chains. Given any needle N = {(x; y; z); (x0; y0; z0)}, let N =
{(x; y); (x0; y0)} denote the shadow of needle N on the oor. For Ana (who is an artist), a
oor closed chain is also a sequence of k distinct needles, N1;N2; : : : ;Nk (for some k >= 3),
such that:
 Ni != Nj , for every 1 <= i < j <= k (the k needle shadows are all distinct);
 N1 = {(x1; y1); (x2; y2)}; N2 = {(x2; y2); (x3; y3)}; : : : ;
Nk = {(xk; yk); (xk+1; yk+1)}; and (xk+1; yk+1) = (x1; y1).
Consider the sculpture depicted in the gure, which has the following four knitting needles:

A = {(12; 12; 8); (10; 5; 11)}; B = {(12; 12; 8); (4; 14; 21)};
C = {(12; 12; 8); (12; 20; 8)}; D = {(4; 14; 21); (10; 5; 21)}:
This structure is not free of closed chains because, although there is no true closed chain, the
sequence of needles A;B;D is a oor closed chain.


Task

Write a program that, given the knitting needles of a sculpture, determines whether there is
a true or a oor closed chain in the sculpture.


Input

The rst line of the input has one integer, K, which is the number of knitting needles in the
sculpture. Each of the following K lines contains six integers, x1, y1, z1, x2, y2, and z2, which
indicate that f(x1; y1; z1); (x2; y2; z2)g is the set of triples of a needle. Any two distinct needles
are represented by dierent sets of triples.
Constraints
1  K  50 000 Number of knitting needles
1  xi; yi; zi < 1 000 Coordinates of each triple


Output

The output has two lines, each one with a string. The string in the rst line is: True
closed chains, if there is some true closed chain in the sculpture; No true closed chains,
otherwise. The string in the second line is: Floor closed chains, if there is some oor closed
chain in the sculpture; No floor closed chains, otherwise.


Sample Input 1

4
12 12 8 10 5 11
12 12 8 4 14 21
12 12 8 12 20 8
4 14 21 10 5 21

Sample Output 1

No true closed chains
Floor closed chains

Sample Input 2

4
1 1 1 2 2 2
2 2 2 1 5 5
9 4 4 9 4 2
9 4 4 9 9 4

Sample Output 2

No true closed chains
No floor closed chains

Sample Input 3

3
50 50 50 100 100 100
100 100 100 50 50 90
50 50 90 50 50 50

Sample Output 3

True closed chains
No floor closed chains

Sample Input 4

3
1 1 5 1 3 7
1 3 7 4 4 5
4 4 5 1 1 5

Sample Output 4

True closed chains
Floor closed chains


Source

SWERC'2016 Universidade do Porto

Gym - 101174K


My Solution

题意:给出三维空间的n条边,每个点都是三维坐标,分别判断三维图有没有环,以及投影到xy坐标平面有没有环。


dfs

对于三维图只要建图后用dfs跑一遍即可, 注意{u = (x, y, z), v = (x, y, z)}这样的边(点)不能放到图上,

对于二维图则在不把{u = (x, y), v = (x, y)}不放到图上之外,每次跑dfs的时候还要用unique去重,//sz = unique(sons[u].begin(), sons[u].end()) - sons[u].begin();

因为在跑dfs的时候,我们是把第二次遇到vis[u]时定义为有环,但如果sons[u]里面有好几个相同的v时,没有环的时候,也会再次遇到vis[u],所以这里要去重。

比如:

3

{(1, 1, 2), (1, 2, 3)}

{(1, 1, 3), (1, 2, 4)}

{(1, 1, 4), (1, 2, 5)}

此外这里需要判断有没有的环是至少三元环。

复杂度 O(2*n)


#include <iostream>#include <cstdio>#include <vector>#include <map>#include <cstring>#include <algorithm>using namespace std;typedef long long LL;typedef pair<int, int> ii;typedef pair<int, pair<int, int>> iii;const int MAXN = 5e4 + 8;int x[MAXN], y[MAXN], z[MAXN], x2[MAXN], y2[MAXN], z2[MAXN];map<iii, int> mpiii;map<ii, int> mpii;vector<int> sons[2*MAXN];bool f[2*MAXN], vis[2*MAXN], ans1, ans2;inline bool dfs(int u, int fa, int type){    int sz = sons[u].size(), v, i;    sz = unique(sons[u].begin(), sons[u].end()) - sons[u].begin();    for(i = 0; i < sz; i++){        v = sons[u][i];        if(v == fa) continue;        if(vis[v]){            if(type == 1) ans1 = true;            else ans2 = true;            return true;        }        if(f[v]){            continue;        }        vis[v] = true;        f[v] = true;        if(dfs(v, u, type)){            return true;        }        else{            vis[v] = false;        }    }    return false;}int main(){    #ifdef LOCAL    freopen("k.txt", "r", stdin);    //freopen("k.out", "w", stdout);    int T = 5;    while(T--){    #endif // LOCAL    ios::sync_with_stdio(false); cin.tie(0);    int n, i, u, v, w, totiii = 1, totii = 1;    ans1 = false, ans2 = false;    cin >> n;    for(i = 1; i <= n; i++){        cin >> x[i] >> y[i] >> z[i] >> x2[i] >> y2[i] >> z2[i];    }    for(i = 1; i <= n; i++){        if(mpiii.find(iii(x[i], ii(y[i], z[i]))) == mpiii.end()){            mpiii[iii(x[i], ii(y[i], z[i]))] = totiii;            totiii++;        }        if(mpiii.find(iii(x2[i], ii(y2[i], z2[i]))) == mpiii.end()){            mpiii[iii(x2[i], ii(y2[i], z2[i]))] = totiii;            totiii++;        }        u = mpiii[iii(x[i], ii(y[i], z[i]))];        v = mpiii[iii(x2[i], ii(y2[i], z2[i]))];        if(u != v){            sons[u].push_back(v);            sons[v].push_back(u);        }        //cout << u << " " << v << endl;    }    memset(f, false, sizeof f);    memset(vis, false, sizeof vis);    for(i = 1; i < totiii; i++){        if(ans1) break;        if(f[i]) continue;        dfs(i, -1, 1);    }    for(i = 1; i < totiii; i++){        sons[i].clear();    }    mpiii.clear();    for(i = 1; i <= n; i++){        if(mpii.find(ii(x[i], y[i])) == mpii.end()){            mpii[ii(x[i], y[i])] = totii;            totii++;        }        if(mpii.find(ii(x2[i], y2[i])) == mpii.end()){            mpii[ii(x2[i], y2[i])] = totii;            totii++;        }        u = mpii[ii(x[i], y[i])];        v = mpii[ii(x2[i], y2[i])];        if(u != v){            sons[u].push_back(v);            sons[v].push_back(u);        }    }    memset(f, false, sizeof f);    memset(vis, false, sizeof vis);    for(i = 1; i < totii; i++){        if(ans2) break;        if(f[i]) continue;        dfs(i, -1, 2);    }    if(ans1){        cout << "True closed chains" << endl;    }    else{        cout << "No true closed chains" << endl;    }    if(ans2){        cout << "Floor closed chains" << endl;    }    else{        cout << "No floor closed chains" << endl;    }    #ifdef LOCAL    for(i = 1; i < totii; i++){        sons[i].clear();    }    mpii.clear();    cout << endl;    }    #endif // LOCAL    return 0;}


  Thank you!

                                                                                                                                            ------from ProLights

原创粉丝点击