Triangular lamp array topological sorting

Subject description:

Mid-Autumn Festival night, the small x on the table put a lot of nice lights. Unfortunately, these lights may not all are on. So, going to all these small x lights are lit.
However, the small x and soon found that these lights are placed very regularly, in fact, we are in a position lights at the intersection of the plane of the triangular tiling. Lantern unit length distance is considered to be adjacent to each other. As can be seen, each lantern lights up to six adjacent, adjacent lights in the center of the unit on its regular hexagon vertices.
The figure is a legitimate display lights (corresponding to sample data):

Wherein the solid and hollow dots represent the bright lights on and off, the solid line edges represent the relationship between the adjacent lights.
Edge graph consisting of several units of the equilateral triangle (side length of triangular unit length), it can be seen, each lantern lights adjacent to it may be composed of up to six units of an equilateral triangle. And all the units may be divided into two triangles, which we call A and Class Classes B, in FIG shaded triangle belongs to class A. Wherein Class A triangle, three vertexes are at the top, bottom left and bottom right.
A triangle in each class has a switch (not shown), pressing the switch in the switch will change each triangle blinking lights on three vertices of the state (light becomes off, turns bright OFF).
So, to light up all the lights, at least how many times the switch need to press it?

Input Format

Initial input data describes the state of blinking lights, and all of each triangle.

A first line integer N, the number of lights, lights numbered 1 through N.

The second row of N integer of 0 or 1, the i-th initial state 0 or 1 indicates the i-th light or lights are off. The third line of an integer M, M relationship expressed lights adjacent.

The following M rows, each row of three integers representing the relationship between the adjacent lights couple. The first two integers that is adjacent to the number of lights. The third integer second orientation relative to the first lights of lights, each represent 0 to 5, the right, lower right, lower left, left, top left, top right.

For example, "131" represents a "third lights of a lantern in the lower right."

Output Format

If the lights light up all impossible, outputs -1, otherwise the output of the minimum number of steps.

Winter seem to have heard Hailiang Liu Nongfu Spring about this topic, then did not how to listen, to see heavyweights Chdy room to write, so also joined in, found a very interesting topic;

Because the original had a similar impression of the subject I looked so find a topology;

Then we consider how to build the topology map we know that maintenance is a dependency;

For this question we have to consider how it can change the state of the entire lantern lit;

We certainly are looking for a first point, and then expand to other points, when all the points while convenient, we find the minimum number of steps and lit all the lights;

For a Class A vertex of a triangle, if he is not a point lower left lower right of other class A triangle, we consider him from the start, so if he is lit;

We can not change his light and dark where the triangle, because if you change, then the current will be lit this dark, while we can not let down transfer relationship has traversed dark point out again;

So this is the only dark spot is significant, we found a unique solution, so we can state his point and connected to the negated if there is another point to be the current state of the vertex,

That did not use it as a triangle lower left lower right point, we are going from this point on, until all the points traversed;

So we consider for a point, we have to change his status, point to the left of the right is not affected, so we only consider points in the oblique oblique;

Therefore, a point on the x, y is in its upper left if right, we will point y x, x + of penetration;

If you are in the lower right lower left, we will point x y, y of penetration ++;

Topology begin to solve this problem;

As for the other types of solution, such as fzh Gangster middle of the Class A triangle line, a point, then double-edge topology, write up trouble, but he would say relatively clever, all right ah. . Really clever! ! %%%

#include<bits/stdc++.h>
using namespace std;
const int N=50000;
template<typename T>inline void read(T &x) {
    x=0;T f=1,ch=getchar();
    while(!isdigit(ch))  {if(ch=='-')  f=-1;  ch=getchar();}
    while(isdigit(ch))  {x=(x<<1)+(x<<3)+(ch^48);  ch=getchar();}
    x*=f;
}
int n,m,x,y,z,tot,ans,lin[N<<1],s[N],in[N]; 
struct gg {
    int y,next;
}a[N<<1];
inline void add(int x,int y) {
    a[++tot].y=y;
    a[tot].next=lin[x];
    lin[x]=tot;
}
int main() {
    //freopen("1.in","r",stdin);
    read(n);
    for(int i=1;i<=n;i++) {
        read(s[i]);
    }
    read(m);
    for(int i=1;i<=m;i++) {
        read(x); read(y); read(z);
        if(z==1||z==2) add(x,y),in[y]++;
        if(z==4||z==5) add(y,x),in[x]++;
    }
    queue<int>q; 
    for(int i=1;i<=n;i++) {
        if(!in[i]) q.push(i);
    }
    ans=0;
    while(q.size()) {
        int x=q.front();q.pop();
        for(int i=lin[x];i;i=a[i].next){
            int y=a[i].y;
            in[y]--;
            if(!in[y]) q.push(y);
            if(!s[x]) s[y]^=1;
        }
        if(!s[x]) ans++;
    }
    cout<<ans<<endl;
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/Tyouchie/p/11458569.html
Recommended