洛谷 P2913 [USACO08OCT]车轮旋转Wheel Rotation

题目描述

Farmer John has an old-time thresher (wheat harvester) that requires belts to be installed on various gears to turn the parts. The engine drives pulley 1 in a clockwise direction which attaches via a belt to pulley 2. Pulley 2 attaches via a belt to pulley 3 and so on through a total of N (2 <= N <= 1,000) pulleys (and N-1 belts).

The diagram above depicts the two ways a belt can be installed between two gears. In this illustration, pulley 1's belt directly drives pulley 2 (a 'straight' connection) and thus they will rotate in the same direction. Pulley 3 drives pulley 4 via a 'crossed belt' that reverses the direction of the rotation.

Given a list of the belt types that connect the pulleys along with the fact that pulley 1 is driven in a clockwise direction by the engine, determine the drive direction of pulley N. Each belt is described by three integers:

* S_i -- the driving (source) pulley 
* D_i -- the driven (destination) pulley 
* C_i -- the connection type (0=straight, 1=crossed) 
Unfortunately, FJ lists the belts in random order. 
By way of example, consider the illustration below. N = 4, and pulley 1 is driven clockwise by the thresher engine. Straight 
belts drive pulley 2 and then pulley 3, so they rotate clockwise. The crosswise belt reverses the rotation direction so pulley 4 (pulley N) rotates counterclockwise. 

POINTS: 70 约翰有一个过时的收割机,需要在它的各种滑轮上装配皮带才能让收割机的各个部分运作起 来.引擎能够驱动滑轮1向顺时针方向转动,滑轮1通过一条皮带又连接到滑轮2.滑轮2又通过一 条皮带连接到滑轮3,等等,总共有N(2 <= N <= 1000)个滑轮和N - 1条皮带.

皮带连接两个滑轮有两种方式:直接连接和交叉连接.直接连接的两个滑轮旋转方向相同, 即同为顺时针或同为逆时针.交叉连接的两个滑轮旋转方向相反.

现在给出一个列表,里面列出所有皮带的连接方式.已经知道滑轮1被引擎驱动着向顺时针方 向转动.每一条皮带由下面三个数定义:

•驱动滑轮S,输入驱动力的滑轮.

•被驱动滑轮D;,被驱使转动的滑轮.

•连接类型C,0表示直接连接,1表示交叉连接.

不幸的是,约翰的这个列表中,皮带的顺序是混乱的.所以请你写一个程序来求出滑轮N的 转动方向.

输入输出格式

输入格式:

 

  • Line 1: A single integer: N

  • Lines 2..N: Each line describes a belt with three integers: S_i, D_i, and C_i

 

输出格式:

 

  • Line 1: A single integer that is the rotation direction for pulley N (0=clockwise, 1=counterclockwise)

 

输入输出样例

输入样例#1: 复制
4 
2 3 0 
3 4 1 
1 2 0 
输出样例#1: 复制
1 

说明

As in the example illustration.

思路:搜索。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 1010
using namespace std;
int n,tot;
int vis[MAXN];
int to[MAXN],head[MAXN],net[MAXN],cap[MAXN];
void add(int u,int v,int w){
    to[++tot]=v;cap[tot]=w;net[tot]=head[u];head[u]=tot;
}
void dfs(int now,int fa){
    for(int i=head[now];i;i=net[i])
        if(to[i]!=fa){
            if(cap[i]==0)    vis[to[i]]=vis[now];
            else vis[to[i]]=!vis[now];
            dfs(to[i],now);
        }
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<n;i++){
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        add(x,y,z);
    }
    vis[1]=0;
    dfs(1,0);
    cout<<vis[n];
}

 

posted @ 2017-12-05 20:57  一蓑烟雨任生平  阅读(266)  评论(0)    收藏  举报