青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

posts - 7,comments - 3,trackbacks - 0
Electricity

Time Limit: 10 Seconds      Memory Limit: 32768 KB      Special Judge

After recent blackouts in some regions in North America, the government has decided to reorganize the power supply network of the continent.

The power supply network is the set of nodes, such as power plants or transformation stations, connected by transmission lines. All lines are used to transmit electricity from one node to another. For stability reasons the system is organized in such a way that there are no directed cycles.

Since the government is currently short of money due to several small peaceful militaristic operations, it cannot build new power lines for the moment. So after reorganization the same lines will be used, but some lines will have to transmit electricity in the direction opposite to the current one. To make the reorganization gentle enough, the management of the power network is planning to switch the transmission direction for exactly one line each day. Of course, no day there must be a cycle in a network, since this may cause damage to the system. The resulting network is also designed to be acyclic.

Help them to plan the reorganization.

Input

There are mutilple cases in the input file.

The first line of the input file contains n --- the number of nodes in the network, and m --- the number of transmission lines (2 <= n <= 1,000 , 1 <= m <= 10,000 ). The following m lines contain three integer numbers each. The first two give the source and the destination node for the corresponding line in the current node. The third number is 0 if the line must keep its transmission direction in the resulting network, and 1 if the direction must be reversed.

There can be several lines connecting the same pair of nodes, but due to acyclicity condition, they all transmit electricity in the same direction. This is also the reason why no line connects a node to itself.

There is an empty line after each case.

Output

First output k --- the number of days in the plan you suggest. You don't need to minimize this number, but it must not exceed 4m . After that print k integer numbers --- for each day output the number of the line that changes the transmission direction this day. If it is impossible to make the desired reorganization, output -1 instead of k .

There should be an empty line after each case.

Sample Input

4 5
1 2 0
2 3 1
2 4 1
1 4 1
4 3 0

2 2
1 2 1
1 2 1

Sample Output

3
3 2 4

-1


Source: Andrew Stankevich's Contest #9


一道不錯的圖論題,大意是給一個有向網(wǎng)絡(luò)N,必須將其中X條邊反向,給出一個方案順序,使得在執(zhí)行反向的過程中,網(wǎng)絡(luò)不會出現(xiàn)環(huán),如果不存在這個方案,輸出-1.

思路:首先,判斷不存在的情況:1.兩個重邊都要反轉(zhuǎn),那么在操作過程中肯定出現(xiàn)環(huán);2.在初始網(wǎng)絡(luò)或結(jié)束網(wǎng)絡(luò)存在環(huán)。對于第二種情況,可以用topo排序求一下兩個網(wǎng)絡(luò)是否存在環(huán),并記錄下topp序列。
之后,我們要構(gòu)造方案,構(gòu)造方案的過程有兩個:
1.一個點的可逆入度邊(這句是廢話,可逆邊就是必須改變的邊,肯定包含于方案,但注意是入度)
2.這個點必須從結(jié)尾網(wǎng)絡(luò)的topo序列從后往前搜索.....(凌亂了吧....)
首先,我們要明白topo序列的性質(zhì),就是序列a1,a2,a3,...,an,表示的是網(wǎng)絡(luò)n個點的線性關(guān)系,存在任意的i<j,使得ai -> aj,也就是,如果用網(wǎng)絡(luò)表示topo序列,那么只有往右邊指向的邊。
通過這一個性質(zhì),加上網(wǎng)絡(luò)N前后兩次的topo序列,我們不難發(fā)現(xiàn),結(jié)尾網(wǎng)絡(luò)topo序列的最后一個肯定是在操作中失去出度而從初始的topo序列降為(或停留)最后面,所以,將其可反轉(zhuǎn)的入度邊反向,肯定不會存在回射邊從而產(chǎn)生環(huán)結(jié)構(gòu),因此,從最后一個點向前搜索,每次執(zhí)行可執(zhí)行的反轉(zhuǎn)操作,那么一定能保持當(dāng)前點的不存在回射邊。
對于一個點,可存在同時支配多條可反轉(zhuǎn)邊,因為答案要求一次一次執(zhí)行反轉(zhuǎn),如果對于同一個點順序不當(dāng)可能出現(xiàn)環(huán),所以我們考慮以下問題:x->y, x->z,如果反轉(zhuǎn)(x,y)從而導(dǎo)致了環(huán)的出現(xiàn),那么可以肯定z->y是成立的,而在topo關(guān)系上z比y靠前,所以我們對于同一個點輸出結(jié)果時,要按照其初始網(wǎng)絡(luò)的topo順序,從左向右輸出。
思路完畢,AC,證明....略了吧,我證明了一草稿紙。
注意不要用矩陣,我因為那個吃了幾次CE......
代碼:
#include <iostream>
#include 
<cstdio>
#include 
<cstring>
#include 
<queue>
#include 
<vector>
#define N 1100
#define M 10010
using namespace std;

int n, m;

struct edge
{
    
int u, v, next;
} et[
2][M];

int eh[2][N], tot[2];
int be[2 * N], ed[2 * N], sta[2 * N];
int deg[2][N], deg2[N];
int g[N][N];

void add(int u, int v, int i)
{
    
int t = ++tot[i];
    et[i][t].u 
= u;
    et[i][t].v 
= v;
    et[i][t].next 
= eh[i][u];
    eh[i][u] 
= t;
    deg[i][v]
++;
}

int topo(int eh[], edge et[], int que[], int deg[])
{
    
int i, j, k, top = -1, qt = 0;
    
for (i = 1; i <= n; ++i)
        deg2[i] 
= deg[i];
    
for (i = 1; i <= n; ++i)
        
if (deg2[i] == 0)
            sta[
++top] = i;
    
for (j = 1; j <= n; ++j)
    {
        
if (top == -1return 0;
        
int u = sta[top--];
        que[qt
++= u;
        
for (i = eh[u]; i != -1; i = et[i].next)
        {
            deg2[et[i].v]
--;
            
if (deg2[et[i].v] == 0) sta[++top] = et[i].v;
        }
    }
    
return 1;
}

int was[M], cnt;
void slove()
{
    
int i, j, k;
    memset(was, 
0sizeof(was));
    printf(
"%d\n", cnt);
    
for (i = n - 1; i >= 0--i)
    {
        
int u = ed[i];
        
for (j = 0; j < n; ++j)
          
if (g[u][be[j]] > 0)
          {
              
if (was[g[u][be[j]]] == 0)
              {
                  was[g[u][be[j]]] 
= 1;
                  printf(
"%d ", g[u][be[j]]);
              }
          }
    }
    
if (cnt > 0) printf("\n");
}

int main()
{
    
int i, j, k;
    
while (scanf("%d%d"&n, &m) != EOF)
    {
        memset(eh, 
-1sizeof(eh));
        memset(deg, 
0sizeof(deg));
        
for (i = 1; i <= n; ++i)
          
for (j = 1; j <= n; ++j)
            g[i][j] 
= 0;
        tot[
1= tot[0= 0;
        cnt 
= 0;
        
int flag = 1;
        
for (i = 1; i <= m; ++i)
        {
            
int a, b, c;
            scanf(
"%d%d%d"&a, &b, &c);
            add(a, b, 
0);
            
if (c)
            {
                add(b, a, 
1);
                cnt
++;
                
if (g[a][b] > 0) flag = 0;
                g[a][b] 
= i;
            }
            
else
              add(a, b, 
1);
        }
        
if (flag == 0)
        {
            printf(
"-1\n\n");
            
continue;
        }
        
int first = topo(eh[0], et[0], be, deg[0]);
        
int last = topo(eh[1], et[1], ed, deg[1]);
        
if (!(first && last))
        {
            printf(
"-1\n\n");
            
continue;
        }
        slove();
        printf(
"\n");
    }
    
return 0;
}
posted on 2011-10-15 22:12 LLawliet 閱讀(159) 評論(0)  編輯 收藏 引用 所屬分類: 圖論
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲欧美在线另类| 午夜天堂精品久久久久| 欧美电影免费观看高清| 免费欧美日韩| 99在线精品视频| 99精品国产福利在线观看免费| 欧美日韩精品免费观看视一区二区| 亚洲手机成人高清视频| 亚洲一区二区伦理| 一区二区在线看| 亚洲国产一区二区三区青草影视| 欧美精品在线极品| 久久福利毛片| 免费永久网站黄欧美| 亚洲一区二区三区欧美| 欧美一区二区国产| 亚洲美女av在线播放| 亚洲永久免费av| 最新高清无码专区| 亚洲性夜色噜噜噜7777| 在线观看91久久久久久| 一本到高清视频免费精品| 国产在线一区二区三区四区 | 国产欧美日韩一区| 蜜臀99久久精品久久久久久软件 | 欧美一区二区视频在线观看| 亚洲激情在线| 亚洲男人av电影| 亚洲乱码国产乱码精品精98午夜| 亚洲综合精品自拍| 亚洲人成网站777色婷婷| 亚洲综合视频1区| 日韩天堂在线视频| 久久午夜精品| 久久国产精品网站| 欧美日韩精品二区第二页| 蜜桃av噜噜一区| 国产亚洲一区在线| 一区二区三区视频在线播放| 亚洲国产第一页| 久久gogo国模啪啪人体图| 亚洲夜间福利| 欧美理论在线播放| 欧美激情精品| 亚洲第一黄色网| 欧美在线播放一区| 欧美一区二区三区电影在线观看| 欧美男人的天堂| 欧美激情91| 亚洲国产一区视频| 久久中文久久字幕| 老色鬼精品视频在线观看播放| 国产麻豆日韩| 亚洲综合色在线| 性欧美超级视频| 国产精品盗摄一区二区三区| 日韩午夜三级在线| 一区二区电影免费观看| 欧美欧美在线| 亚洲美女精品成人在线视频| 日韩视频第一页| 欧美精品久久一区二区| 亚洲人午夜精品免费| 亚洲美女诱惑| 亚洲精选成人| 欧美国产日韩精品| 最新热久久免费视频| 亚洲精品看片| 欧美久久久久久久久| 一道本一区二区| 午夜精品久久| 国产一区二区日韩精品欧美精品| 午夜一级在线看亚洲| 久久久国产精彩视频美女艺术照福利| 国产有码在线一区二区视频| 久久精品国产视频| 欧美成人精品一区| 日韩视频在线观看| 国产精品精品视频| 欧美亚洲网站| 欧美国产综合一区二区| 夜夜嗨av一区二区三区四季av| 欧美日韩在线精品| 欧美一乱一性一交一视频| 免播放器亚洲一区| 亚洲视频在线观看| 国产原创一区二区| 欧美成人精品福利| 一区二区精品| 老色鬼久久亚洲一区二区| 亚洲免费大片| 国产区精品在线观看| 免费在线观看一区二区| 这里只有精品视频| 蜜桃av一区二区三区| 亚洲午夜高清视频| 韩国三级电影一区二区| 欧美日韩八区| 久久九九免费视频| 一区二区日韩| 欧美国产视频一区二区| 午夜亚洲性色视频| 99精品视频免费全部在线| 国产人妖伪娘一区91| 欧美巨乳波霸| 久久久久成人精品| 亚洲少妇一区| 亚洲激情欧美激情| 久久一区精品| 欧美亚洲一区在线| 一本一本久久a久久精品综合麻豆 一本一本久久a久久精品牛牛影视 | 国产欧美视频一区二区| 欧美精品电影在线| 欧美一区在线看| 99国产麻豆精品| 亚洲二区在线观看| 久久综合久久综合久久| 欧美一区二区三区四区在线观看地址 | 午夜精品福利视频| 99在线精品免费视频九九视| 欧美成人精品激情在线观看| 久久精品国产亚洲a| 亚洲中字黄色| 一区二区三区日韩| 日韩午夜在线播放| 亚洲欧洲一区二区在线播放| 激情视频一区二区三区| 国产美女精品人人做人人爽| 欧美日韩精品三区| 欧美日韩成人精品| 欧美精品久久99| 欧美精品亚洲精品| 欧美另类在线观看| 欧美激情精品久久久六区热门 | 夜夜嗨av色一区二区不卡| 亚洲电影免费在线| 亚洲国产精品一区二区三区| 欧美va亚洲va香蕉在线| 欧美成人在线影院| 亚洲第一视频| 亚洲国产精品成人久久综合一区| 欧美大片免费观看| 欧美激情91| 亚洲精品日本| 一本色道久久综合亚洲精品按摩| 日韩午夜电影在线观看| 亚洲视频一区二区| 亚洲欧美日韩一区二区在线| 亚洲欧美美女| 久久精品视频va| 老司机久久99久久精品播放免费| 美日韩精品免费| 欧美日韩国产在线播放| 欧美性做爰猛烈叫床潮| 国产精品你懂的| 国外成人在线| 亚洲精品在线观看免费| 亚洲视频香蕉人妖| 欧美一区日韩一区| 久久尤物电影视频在线观看| 欧美成人午夜激情| 最新亚洲电影| 亚洲欧美日韩高清| 久久色在线播放| 欧美日韩免费网站| 国产麻豆成人精品| 亚洲韩国青草视频| 午夜视频在线观看一区二区| 久久综合99re88久久爱| 亚洲日本电影| 欧美在线综合视频| 欧美精品一区二区三区蜜桃 | 久久尤物视频| 欧美日韩在线直播| 好看不卡的中文字幕| 亚洲精品欧美在线| 欧美在线观看视频在线| 亚洲韩国日本中文字幕| 亚洲欧美精品在线观看| 免费看的黄色欧美网站| 国产精品亚洲欧美| 亚洲欧洲在线视频| 久久久高清一区二区三区| 亚洲另类在线视频| 久久久精品一品道一区| 国产精品国产三级国产专区53| 亚洲第一综合天堂另类专| 亚洲欧美一区二区三区极速播放 | 亚洲高清视频在线| 欧美一区2区视频在线观看| 欧美日韩的一区二区| 在线精品视频一区二区| 香蕉久久国产| av成人免费在线观看| 欧美ed2k| 好吊日精品视频| 欧美一区二区三区婷婷月色| 日韩一级裸体免费视频| 欧美 亚欧 日韩视频在线| 国内自拍亚洲|