上海交大-“Code人心弦”9月网络赛-OddGraph

简介: 给你一个无向图,如果所有顶点的度都为奇数,则输出ODD GRAPH,否则找该图的子图是否存在满足条件的,存在则输出最少顶点数和对应顶点的序号,否则输出NO ODD SUBGRAPH。

1098. OddGraph

Description


Pangzi looks somehow odd recently. He started to love odd things, like odd numbers and odd graphs. He says, an odd graph is a non-empty graph (that is the set of vertices is non-empty) that every vertex has an odd degree (that is, connected to an odd number of edges). If a graph is not odd, he will try to find a subgraph that is odd. In Pangzi's mind, a subgraph of a graph G=(V, E) is composed by some vertices in G and all edges between these vertices from G. Mathematically, G'=(V', E') where V' is a subset of V and E'={(u, v): u, v in V' and (u, v) in E}.


Now Pangzi will give you a graph, please tell him if this graph is odd. If it is not odd, try to find an odd subgraph.


Pay attention. As Pangzi is odd now, when he talks about graphs, he is actually talking about undirected graphs.


Input Format


The first line of the input is an integer T (T <= 100), indicating the number of test cases.


Then, T test cases follow. For every test case, the first line is 2 integers n, m (1 <= n <= 100, 0 <= m <= 1000). Then m lines follow, every line contains 2 integers u, v (1<=u, v<=n) indicating an edge in G.


There is no self-loop or parallel edge.


Output Format


Output the answer for each test case.


  • If the graph is an odd graph, output "ODD GRAPH".
  • If the graph is not odd and contains an odd subgraph, output the subgraph in the following format. First output a number K, the vertices in this subgraph, and then output K integers in increasing order representing the vertices. These numbers should be printed in one line and separated by one space. No extra spaces. If there are multiple solutions, first minimize K, and if there are still multiple solutions, output the lexicographically smallest sequence of vertices.
  • If the graph is not odd and contains no odd subgraph, output "NO ODD SUBGRAPH"


For two sequences a[1], a[2], ..., a[K] and b[1], b[2], ..., b[k], a is lexicographically smaller than b if and only if for the smallest i such that a[i] != b[i], a[i] < b[i].


See the sample output for clarifications.


Sample Input


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

Sample Output



NO ODD SUBGRAPH
2 1 2
ODD GRAPH


Case Limits


Time limit: 500 msec


Memory limit: 64 MB


题意:给你一个无向图,如果所有顶点的度都为奇数,则输出ODD GRAPH,否则找该图的子图是否存在满足条件的,存在则输出最少顶点数和对应顶点的序号,否则输出NO ODD SUBGRAPH。


思路:用x[i]记录i顶点的度,map[i][j]记录顶点i和j之间有几条边,遍历x数组判断是否为ODD GRAPH,是则输出,否则继续判断子图,若存在,最小子图的顶点数必然为2,因此从顶点1往下找即可。


#include<stdio.h>
#include<string.h>
int map[122][122];
int x[122];
int main()
{ 
  int n,m,t,i,j,a,b;  
  while(scanf("%d",&t)!=EOF)  
  {   
    while(t--)    
    {     
      scanf("%d%d",&n,&m);      
      if(m==0)      
      {       
        printf("NO ODD SUBGRAPH\n");        
        continue;   
      }
      memset(x,0,sizeof(x));    
      memset(map,0,sizeof(map));      
      for(i=0;i<m;i++)      
      {       
        scanf("%d%d",&a,&b);        
        x[a]++;       
        x[b]++;       
        map[a][b]++;        
        map[b][a]++;      
      }   
      int flag=0;     
      for(i=1;i<=n;i++) 
      {     
        if(x[i]%2==0)   
        {     
          flag=1;   
          break;    
        }   
      }   
      int flag1,xx,yy;    
      if(flag==1) 
      {     
        flag1=0;  
        for(i=1;i<=n;i++) 
        {         
          for(j=i+1;j<=n;j++)   
          {         
            if(map[i][j]%2==1)  
            {         
              flag1=1;    
              xx=i;     
              yy=j; 
              break;    
            }       
          }       
          if(flag1==1)      
            break;      
        }   
      }   
      else    
        printf("ODD GRAPH\n");    
      if(flag1==1)    
      {     
        printf("2 %d %d\n",xx,yy);    
        flag1=2;      
      }     
      if(flag==0&&flag1==0)       
        printf("NO ODD SUBGRAPH\n");  
    }
  }
  return 0;
}
相关文章
|
负载均衡 前端开发 算法
如何使用Nginx 部署前端项目,什么是反向代理?
Nginx可以作为静态web服务器来部署静态资源。这里所说的静态资源是指在我们web服务端真实存在,并且能够直接展示的一些文件,比如常见的html页面
如何使用Nginx 部署前端项目,什么是反向代理?
|
2月前
|
人工智能 Linux 芯片
【保姆级】Ollama下载安装+本地模型部署+避坑指南一篇搞定
Ollama是本地运行AI模型的轻量工具,支持Windows/macOS/Linux。安装后可离线下载Qwen3:4b等中文模型,无需注册、不联网也能聊天、改写、整理文本,隐私安全,适合个人日常AI辅助。
1225 0
|
4月前
|
人工智能 监控 前端开发
开眼了!AI 应用还从零搭?11.7 万 Star awesome-llm-apps 把 Agent/RAG/MCP 模板摆成夜市
Shubhamsaboo/awesome-llm-apps 是一个可运行的 LLM 应用模板库,覆盖 Agent、RAG、MCP、Voice、Memory、Fine-tuning 等方向,适合开发者找灵感、做原型、学 AI 应用工程化。
376 0
|
11月前
|
SQL 人工智能 自然语言处理
企业落地 AI 数据分析,如何做好敏感数据安全防护?
在 AI 问数时代,数据安全与使用效率并非零和博弈。
|
9月前
|
人工智能 监控 算法
AI英语App的分类
2026年AI英语App已升级为拟人化“数字私人教练”,深度融合ASR、LLM、TTS与多智能体技术。主流分为沉浸口语陪练、考试模拟、游戏化学习及自适应教练四类,依托语音-逻辑-语音闭环、RAG知识库与Agentic UI,实现音素级纠音、多口音切换与情境化交互。(239字)
|
11月前
|
前端开发 CDN
微信小游戏的开发费用
微信小游戏开发费用因美术、玩法等差异大,轻量级3-8万,精品中度10-35万,重度游戏达50万以上。另需考虑软著、版号、服务器及买量等硬成本,运营投入常超开发。2026年混合变现与首发激励成新趋势。#微信游戏 #游戏外包
|
人工智能 自然语言处理 API
Hologres × PAI × DeepSeek 搭建 RAG 检索增强对话系统
本文介绍如何使用PAI-EAS部署基于DeepSeek大模型的RAG(检索增强生成)服务,并关联Hologres引擎实例。Hologres与阿里云自研高性能向量计算软件库Proxima深度整合,支持高性能、低延时的向量计算能力。通过PAI-EAS,用户可以一键部署集成了大语言模型和RAG技术的对话系统服务,显著缩短部署时间,并提高问答质量。部署步骤包括准备Hologres向量检索库、部署基于DeepSeek的RAG服务、通过WebUI进行模型推理验证,以及通过API调用进行模型推理验证。Hologres还提供了特色功能支持,如高性能向量计算等。
|
存储 安全 Java
synchronized原理详解(通俗易懂超级好)
当系统检查到锁是重量级锁之后,会把等待想要获得锁的线程进行阻塞,被阻塞的线程不会消耗cpu。但是阻塞或者唤醒一个线程时,都需要操作系统来帮忙,这就需要从用户态转换到内核态,而转换状态是需要消耗很多时间的,有可能比用户执行代码的时间还要长。
synchronized原理详解(通俗易懂超级好)